4.21.2013

Odd Number Triangle

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

 0
 1 0 1
 2 1 0 2 1
 3 2 1 0 1 2 3

Ans.

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

The output of above program would be:

Output of number triangle C program
Figure: Screen shot for number triangle C program

No comments:

Post a Comment