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:
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:
Figure: Screen shot for number triangle C program |
0 comments:
Post a Comment