Q. Write a C program to print the following number pyramid:
1
123
12345
1234567
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,p=1;
printf("Enter number of rows: ");
scanf("%d", &num);
for(r=1; r<=num; r++,p=p+2)
{
for(c=1; c<=p; c++)
printf("%d",c);
printf("\n");
}
return 0;
}
The output of above program would be:
1
123
12345
1234567
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,p=1;
printf("Enter number of rows: ");
scanf("%d", &num);
for(r=1; r<=num; r++,p=p+2)
{
for(c=1; c<=p; c++)
printf("%d",c);
printf("\n");
}
return 0;
}
The output of above program would be:
Figure: Screen shot for number pyramid C program |
Comments
Post a Comment