9.17.2012

Number Pyramid

Q. Write a C program for print the following number triangle:
or
Q. Write a C program for display the following number pyramid:

 5
 45
 345
 2345
 12345

Ans.

/*c program for number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,num,n,i;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 n = num;
 for(r=1; r<=num; r++,n--)
 {
  for(i=n; i<=num; i++)
     printf("%d", i);
  printf("\n");
 }
 getch();
 return 0;
}


The output of above program would be:


Output of number pyramid C program
Screen shot for number pyramid C program

2 comments:

  1. can you please tell me how to print this
    suppose user enter num is 4 then output should be
    10
    9 6
    8 5 3
    7 4 2 1
    73
    62

    ReplyDelete
    Replies
    1. Can you elaborate your program. If user enter number is 5, then what should be output?

      Delete