Print the following pattern for the given number of rows.
Note: N is always odd.
Pattern for N = 5

The dots represent spaces.
N (Total no. of rows and can only be odd)
Pattern in N lines
Constraints :
1 <= N <= 49
Sample Input 1:
5
Sample Output 1:
*
***
*****
***
*
Sample Input 2:
3
Sample Output 2:
*
***
*
Solution
#include <iostream>
#include<cmath>
using namespace std;
int main() {
// Write your code here
int n;
cin>>n;
int i=0;
while(i<=(n+1)/2){
int k=0;
while(k<(n/2-i+1)){
cout<<" ";
k=k+1;
}
int j=0;
while(j<i){
cout<<"*";
j=j+1;
}
j=i-2;
while(j>=0){
cout<<"*";
j=j-1;
}
cout<<endl;
i=i+1;
}
for (int i=n/2;i>0;i--){
for(int k=n/2;k<n-i;k++){
cout<<" ";
}
for (int j=2*i-1;j>0;j--){
cout<<"*";
}
cout<<endl;
}
}
Comments
Post a Comment