jeudi 29 janvier 2015

Drawing a Vertical Pyramid in C

I have to get the desired output as follows:



1
2 6
3 7 10
4 8 11 13
5 9 12 14 15


But I can't seem to figure out how to do it. All I get is:



1
2 6
3 7 6
4 8 7 6
5 9 8 7 6


Here is my code:



#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<i;j++)
{
if((j+1)==1)
printf("%d ",i);
else
printf("%d ",i+n-j);
}
printf("\n");
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire