Code: Diamond of stars

 Code: Diamond of stars

Send Feedback
Note: N is always odd.


Pattern for N = 5


The dots represent spaces.



Input format :
N (Total no. of rows and can only be odd)
Output format :
Pattern in N lines
Constraints :
1 <= N <= 49
Sample Input 1:
5
Sample Output 1:
  *
 ***
*****
 ***
  *
Sample Input 2:
3
Sample Output 2:
  *
 ***
  *

#include<iostream>
using namespace std;


int main()
{
int N;
cin>>N;
int i=1;
int n = (N +1)/2;
int m = (N-1)/2;
while (i<=n)
{
int spaces = 1;
while(spaces<=n-i)
{
cout<<" ";
spaces++;
}
int j =1;
while(j<=(2*i-1))
{
cout<<"*";
j++;
}
i++;
cout<<endl;
}
i=1;
while(i<=m)
{
int spaces = 1;
while(spaces<=2*i-1)
{
cout<<" ";
spaces++;
}
int j=1;
while(j<=N-2*i)
{
cout<<"*";
j++;
}
i++;
cout<<endl;
}
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas