Hollow Diamond Pattern Program in C++

Hello Learners, in this article, we are going to learn how to print a Hollow Diamond Pattern using C++. This pattern is possible and easy to understand in C++.
let us understand first what is a Hollow Diamond?
We will print this pattern by using asterisks/stars (*). The size of the pattern will be dependent on the user in how many rows he/she wants in this particular pattern. In this pattern, a diamond-shaped border made by stars will be there with vacant spaces in between. For example:

Hollow Diamond Pattern Program in C++

The above Hollow Diamond Pattern is containing 5 rows which will be entered by the user.

C++ program to print Hollow Diamond Pattern

We can print this pattern by using “for loop”. Firstly, we will print spaces in decreasing order to obtain the required shape. We will write codes of spaces and stars only for half of the pattern as the lower part of the pattern is a mirror image of the upper part. So, if we reverse the first loop then we will get the same shape as of upper one. Let us understand this by source code:

#include<iostream>
using namespace std;

int main()
{
  int i,j,r;
  cout<<"Enter no. of rows: ";
  cin>>r;
  for(i=1;i<=r;i++)
  {
    for(j=1;j<=r-i;j++)
    cout<<" ";
    for(j=1;j<=2*i-1;j++)
    {
         if(j==1 || j==(2*i-1))
    cout<<"*";
    else
    cout<<" ";
  }
    cout<<"\n";
    
    
  }
    for(i=r;i>=1;i--)
  {
    for(j=1;j<=r-i;j++)
    cout<<" ";
    for(j=1;j<=2*i-1;j++)
    {
     if(j==1 || j==(2*i-1))
    cout<<"*";
    else
    cout<<" ";
  }
    cout<<"\n";
    
  }
  return 0;
}

In the above code i, j are the integer variables that will control the loops and r is the number of rows entered by the user.
i-loop will control rows of this pattern whereas j-loop will print spaces and stars both. We will need to apply some logic in the printing of those stars and spaces which are already given in codes. By tracing them, you can easily get your hands on them. It is written in a very minimal and easy way which makes it fun to learn.

Now, let us see an example in the output screen after executing it:

Enter no. of rows: 10

C++ program to print Hollow Diamond Pattern

In this way, you can print a Hollow Diamond by using C++ language. I hope it was easy enough to understand. If you have any doubt, feel free to ask in the comment section. All the best!

Thank you!

Regards,
Isha Rani Prasad
Codespeedy Tech Pvt. Ltd.

One response to “Hollow Diamond Pattern Program in C++”

  1. Vishal kumar says:

    Thnq for explaining it in easiet way.. Keep uploading more posts like this. Once again thanks you

Leave a Reply

Your email address will not be published. Required fields are marked *