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.
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;
}
Submitted by utkarsh bhadauria (utk251199)
Download packets of source code on Coders Packet
Comments