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