Rhombus Pattern(C++)
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.
- We traverse the rows from 1 to n with a variable i.
- Using nested loops, in each row we print the number of spaces(which is n-row_number)
- 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;
}
Project Files
/
Loading...
| .. | ||
| This directory is empty. | ||