9.09.2012

Number Pyramid

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

       1
      222
     33333
    4444444
   555555555

Ans.

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

/**************Output**************/

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

6 comments:

  1. how to create C PROGRAM. Please Help
    1
    3 5 7
    9 11 13 15 17

    ReplyDelete
    Replies
    1. Source code for above program at:

      http://www.cprogrammingcodes.blogspot.in/2012/10/odd-number-series-pyramid.html

      Delete
  2. how to create C PROGRAM. Please Help
    1
    3 5 7
    9 11 13 15 17

    ReplyDelete
    Replies
    1. @ Poorna Chandran

      Source code for above number pyramid C program :

      http://www.cprogrammingcodes.blogspot.com/2012/10/odd-number-series-pyramid.html

      Delete
  3. How to create program?

    1
    3 5
    7 9 11

    ReplyDelete
    Replies
    1. #Kajal Singh

      Your required odd number pyramid C program source code:

      #include"stdio.h"
      int main()
      {
      int r,c,num=3,p=1;
      for(r=1; r<=num; r++)
      {
      for(c=1; c<=r; c++,p=p+2)
      printf("%d ",p);
      printf("\n");
      }
      getch();
      return 0;
      }

      /**************Output*************/
      1
      3 5
      7 9 11

      Delete