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