Coders Packet

Rhombus Pattern(C++)

By utkarsh bhadauria

Printing a star(*) pattern in the shape of a rhombus with the size given by the user in C++.

Problem Statement - 

 

Given an integer n, print a rhombus of size n.

 

Example - 

 

Input - 

 

N = 5 

 

Output -  

 

    *****

   *****

  *****

 *****

*****

 

Algorithm - 

 

We observe that the number of spaces in each column is equal to n-row_number. Hence, the number of spaces decrements by 1 as we traverse the rows. Using these observations, we use the following algorithm.

 

  1. We traverse the rows from 1 to n with a variable i.
  2. Using nested loops, in each row we print the number of spaces(which is n-row_number)
  3. In the same row, using another nested loop we print n stars.

 

Code -

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
            cout<<" ";
        }
        for(int j=1;j<=n;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by utkarsh bhadauria (utk251199)

Download packets of source code on Coders Packet