Q. Write a C program to print the following number pyramid as:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,sp;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=1; sp<r; sp++)
printf(" ");
for(c=r; c<=num; c++)
printf("%d ",c);
printf("\n");
}
return 0;
}
The output of above program would be:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,sp;
printf("Enter any number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=1; sp<r; sp++)
printf(" ");
for(c=r; c<=num; c++)
printf("%d ",c);
printf("\n");
}
return 0;
}
The output of above program would be:
Figure: Screenshot for number pyramid C program |
No comments:
Post a Comment