Wednesday, 3 July 2013

Number Triangle

Q. Write a C program to print the following number triangle/pyramid program:

1
12
123
1234
123
12
1

Ans.

/*c program for number triangle*/
#include<stdio.h>
int main()
{
 int num,n,r,c,z;
 printf("Enter max. number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
     printf("%d",c);
   printf("\n");
 }

 for(r=1; r<num; r++)
 {
   for(c=num,z=1; c>r; c--,z++)
     printf("%d",z);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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


No comments:

Post a Comment