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:
****************************************************************/
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:
****************************************************************/
Figure: Screen shot for Positive-Negative Number Triangle C program |
Comments
Post a Comment