Diamond pattern program in C++
Written by
Program to print Diamond pattern in C++
In this example, we approach to draw a diamond shape. An illustration of the diamond is as given below:
* *** ***** *** *
Logic:
To print a diamond triangle in C++ we have to combine the logic of Pascal’s triangle and inverted Pascal’s triangle.
In the first part, we’ll print the upper triangle and in the second part we’ll print the lower triangle.
Code:
#include <iostream>
using namespace std;
int main()
{
int i,j,k,space,n;
cout<<"Enter number of rows: ";
cin>>n;
space = n - 1;
cout<<"\n";
//printing the upper triangle
for (i = 1; i <= n; i++)
{
for (j = 1; j<= space; j++)
cout<<" ";//printing spaces
space--;
for (k = 1; k <= 2 * i - 1; k++)
cout<<"*";
cout<<"\n";
}
//printing the lower triangle
space = 1;
for (i = 1; i<= n - 1; i++)
{
for (j = 1; j <= space; j++)
cout<<" ";
space++;
for (k = 1 ; k <= 2 * (n - i) - 1; k++)
cout<<"*";
cout<<"\n";
}
return 0;
}
Output:
Enter number of rows: 5
*
***
*****
*******
*******
*****
***
*