HOW TO SOLVE (N-rows ) DIAMOND PATTERN USING C++.
suryansh taragi
Posted on September 9, 2022
(N-ROWS) DIAMOND OF STARS PATTERN
loops are the most important part of any programming. to master the the concept of loops I practiced various type of pattern (Star,Alphabet,Numeric) .
This question is a last question of assignment in coding-ninjas platform .
**Print the following pattern for the given number of rows.
Note: N is always odd.
Pattern for N = 5
The dots represent spaces.
**
I divided the pattern in 4 parts the upper left,The upper right,
The bottom left and the bottom right . which makes it very easy to solve the given problem.so there are total 6 loops
- the loop which count the rows of upper triangle (n+1/2) .
- the loop which count the rows of bottom triangle (n/2).
- the loop to print spaces for upper triangle (nested loop of upper loop).
- the loop to print spaces for bottom triangle (nested loop of bottom loop).
- the loop to print the * for upper triangle (nested loop of upper loop).
- the loop to print the * for bottom triangle (nested loop of bottom loop).
#include<iostream>
using namespace std;
int main()
{
int i, j, rowNum, space;
cin>>rowNum;
for(i=1; i<=(rowNum+1)/2; i++)
{
for(j=1; j<=(rowNum+1)/2-i; j++)
cout<<" ";
for(j=1; j<=(2*i-1); j++)
cout<<"*";
cout<<endl;
}
for(i=(rowNum/2); i>=1; i--)
{
for(j=1; j<=(rowNum/2)-i+1; j++)
cout<<" ";
for(j=1; j<=2*i-1; j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
return 0;
}
💖 💪 🙅 🚩
suryansh taragi
Posted on September 9, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.