×

Search anything:

# C++ program to print Rhombus Pattern

#### C++ Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article at OpenGenus, we will learn to print Rhombus pattern in C++ language. We will go with two types of Rhombus patterns that is solid Rhombus and hollow Rhombus.

1. Solid Rohmbus Pattern in C++
2. Hollow rohmbus pattern in C++

# 1. Solid Rhombus Pattern in C++

Making a solid square pattern for an i'th row with n-i spaces is quite similar to making a solid rhombus design for any given integer n.

Approach -

1. Initialize variables i and j.
2. Loop from i = 1 to n:
3. Loop from j = 1 to n – i and print spaces.
4. Loop from j = 1 to n and print asterisks.
5. Print a new line.

Code-

``````#include <bits/stdc++.h>
using namespace std;

// Function for Solid Rhombus
void solidRhombus(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
std::cout << " ";
}
for (j = 1; j <= n; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
int main()
{
int rows = 4;
solidRhombus (rows);
return 0;
}
``````

Output -

``````    *****
*****
*****
*****
``````

# 2. Hollow Rhombus Pattern in C++

The concept behind the hollow squares and solid rhombuses is used to create hollow rhombuses. A hollow rhombus is formed when a hollow square has n–i blank spaces before the stars in each ith row.

Approach-

1. Initialize variables i and j.
2. Loop from i = 1 to n:
3. Loop from j = 1 to n – i and print spaces.
4. Loop from j = 1 to n:
5. If i = 1 or i = n or j = 1 or j = n, print an asterisk.
6. Else, print a space.
7. Print a new line.

Code -

``````#include <bits/stdc++.h>
using namespace std;

void hollowRhombus(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
std::cout << " ";
}
for (j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
std::cout << "*";
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
}

int main()
{
int rows = 5;
hollowRhombus (rows);
return 0;
}
``````

Output-

``````    *****
*   *
*   *
*   *
*****
``````

Because there are two nested loops that repeatedly iterate over all the rows and columns of the rhombus, the time complexity of both functions is O(n2).
Due to the fact that no matter how big the rhombus is, only a fixed amount of memory is required, the space complexity is O(1).

With this article at OpenGenus, you must have the complete idea of how to print Rhombus Pattern in C++.