Odd Number Triangle

Wednesday, April 9, 2014

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("
"
);

 }
 getch();
 return 0;
}

The output of above program would be:

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

Related Posts by Categories

0 comments:

Post a Comment

Copyright © 2014 Information Sharing | Powered By Blogger