Q. Write a C program to print the following number triangle as:
1
21
321
4321
54321
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,sp;
printf("Enter no of rows : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=r; c>=1; c--)
printf("%d",c);
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
1
21
321
4321
54321
Ans.
/*c program for number pyramid*/
#include<stdio.h>
int main()
{
int num,r,c,sp;
printf("Enter no of rows : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=r; c>=1; c--)
printf("%d",c);
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
Figure : Screen shot for number triangle C program |
No comments:
Post a Comment