4.21.2013

Odd Number Triangle

Q. Write a C program to print the following number pyramid as:

1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,z;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1,z=1; c<=r; c++,z=z+2)
       printf("%d ",z);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of odd number pyramid C program
Figure: Screen shot for odd number pyramid C program

No comments:

Post a Comment