9.06.2013

Positive-Negative Number Triangle

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

9
8  6
7  5  3
4  2  0 -2
1 -1 -3 -5 -7

Ans.

/*c program for positive-negative number triangle*/
#include<stdio.h>
int main()
{

 int num,n,r,c;
 for(r=1,num=9; r<=5; r++,num--)
 {
   if(r==4)
   {
     for(c=1,n=4; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else if(r==5)
   {
     for(c=1,n=1; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else
   {
     for(c=1,n=num; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   printf("\n");
 }
 getch();
 return 0;
}

/***************************************************************
The output of above positive-negative number triangle C program would be:
****************************************************************/

Output of Positive-Negative Number Triangle C program
Figure: Screen shot for Positive-Negative Number
Triangle C program

1 comment:

  1. Plz Program solve me as output :

    17 16 15 14 13 12 11 10
    9 8 7 6 5 4
    3 2 1 0
    -1 -2
    -3

    ReplyDelete