This C++ program displays a diamond pattern made of asterisks (*). It takes the input value n from the user, which represents the number of rows in the diamond pattern.
The first for loop is responsible for printing the top half of the diamond pattern, starting from the first row and going up to the nth row. The i variable represents the row number. The first inner for loop prints the required number of spaces before printing the asterisks in the left half of the diamond. The second inner for loop prints the asterisks in the left half of the diamond, starting from the second row and going up to the ith row. The third inner for loop prints the asterisks in the right half of the diamond, starting from the first row and going up to the ith row.
The second for loop is responsible for printing the bottom half of the diamond pattern, starting from the first row and going up to the nth row. The i variable represents the row number. The first inner for loop prints the required number of spaces before printing the asterisks in the right half of the diamond. The second inner for loop prints the asterisks in the right half of the diamond, starting from the nth row and going down to the i+1th row. The third inner for loop prints the asterisks in the left half of the diamond, starting from the first row and going up to the (n-i-1)th row
Overall, this program uses three nested for loops to print the diamond pattern. The first two for loops print the top half and bottom half of the diamond pattern, respectively, while the third inner for loop is used twice to print the asterisks in both the left and right halves of the diamond pattern.
#include<iostream> using namespace std; int main() { int i,j,n; cout<<"\nEnter the value:"; cin>>n; for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=(n-i);j++) { cout<<" "; } for(j=1;j<i;j++) { cout<<" *"; } for(j=1;j<=i;j++) { cout<<" *"; } } for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<" "; } for(j=n;j>i;j--) { cout<<" *"; } for(j=1;j<(n-i);j++) { cout<<" *"; } } return 0; }To download raw file Click Here
Enter the value:5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions