DEV Community

Cover image for HOW TO SOLVE (N-rows ) DIAMOND PATTERN USING C++.
suryansh taragi
suryansh taragi

Posted on • Updated on

HOW TO SOLVE (N-rows ) DIAMOND PATTERN USING C++.

(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

Image description

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

  1. the loop which count the rows of upper triangle (n+1/2) .
  2. the loop which count the rows of bottom triangle (n/2).
  3. the loop to print spaces for upper triangle (nested loop of upper loop).
  4. the loop to print spaces for bottom triangle (nested loop of bottom loop).
  5. the loop to print the * for upper triangle (nested loop of upper loop).
  6. 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;
}
Enter fullscreen mode Exit fullscreen mode

git-link

Top comments (0)