Here is a C++ program to print diamond patterns.
Generally, to print a diamond pattern, we need to print two triangles. One in an upward direction and one in reverse, simply print the first triangle and the second triangle in reverse of the first triangle.
To print a diamond pattern of stars in C++ programming, ask the user to enter the number of rows. If the user enters 8 as the row size of the diamond, then its upper-triangular part expands up to 8 lines, whereas its lower-triangular part expands up to 7 (one less than row size) lines.
Get the entered number into a variable rownum. Set another variable space to rownum-1. We are using three for loops
to create each triangle, the upper triangle, and the lower triangle of the diamond.
Consider an example of 8.
rownum = 8; space = 7;
The for loop
started execution i =0 and condition i <= 8 evaluates to be true.
for(i=1; i<=rowNum; i++)
control flow goes inside the loop, now j = 1 and condition j <= 7 evaluates to be true.
for(j=1; j<=space; j++)
inside the loop prints a single space.
cout<<" ";
every time after exiting from this loop, decrement the value of space to print one less space from the next line or row.
space--;
Use the third for loop
to print stars. At first execution, j=1 and the condition j<=(2*i-1) or j<=(2*1-1) or 1<=(1) or 1<=1 evaluates to be true, therefore program flow goes inside this loop and prints a star (*).
for(j=1; j<=(2*i-1); j++)
cout<<"*";
Using endl
, the next thing starts from the new line.
cout<;
this process continues until the condition evaluates to be false. In this way, the star pattern of the upper diamond gets printed, similarly, the lower diamond also gets printed.
Step 1: Call the header file iostream.
Step 2: Use the namespace std.
Step 3: Open the integer type main function; int main().
Step 4: Declare integer type variables rownum, space, I, j;
Step 5: Ask the user to enter the number of rows.
Step 6: Get the number into the variable rownum;
Step 7: Set the value of the variable space to rownum – 1;
Step 8: create a for loop corresponding to rows. Initialize the value of i to 0; Check for the condition i <= rownum. go to step 9 for true;
Step 9: Create a for loop that corresponds to the spaces
Step 10: Create another for loop that corresponds to the printing of stars
With j= 1 at first execution and check for the condition j<=(2*i-1) or j<=(2*1-1) or 1<=(1) or 1<=1. Print a star for true;
Increment the value of j by 1;
Step 11:set space =1;
Step 12: Repeat steps 8, 9, and 10;
Step 13: Exit.
#include<iostream>
using namespace std;
int main()
{
int i, j, rownum, space;
cout<<"Enter the Number of Rows: ";
cin>>rownum;
space = rownum-1;
for(i=1; i<=rownum; i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space--;
for(j=1; j<=(2*i-1); j++)
cout<<"*";
cout<<endl;
}
space = 1;
for(i=1; i<=(rownum-1); i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space++;
for(j=1; j<=(2*(rownum-i)-1); j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
return 0;
}
Enter the Number of Rows: 8 * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** *