Number Pyramid Pattern in C Programming

May 22, 2010 9 Comments » Hits : 130






Generate Following Pattern of Pyramid

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,x=30,y=10;
clrscr();

printf("Enter n (between 2 & 9) : ");
scanf("%d",&n);

 for(i=1;i<=n;i++)
 {
    gotoxy(x,y);
      for(j=1;j<=i;j++)
         printf("%d ",i);
    x=x-1;
    y++;
 }
getch();
}

Output :

Enter n (between 2 & 9) : 9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

Explain ?

gotoxy(x,y);
  •  Gotoxy : interpret it as “goto x y”
  • Moves Cursor Position to Co-Ordinate (x,y)
  • gotoxy(10,40) : moves Cursor Position to Co-ordinate (10,40)
x=x-1;
     y++;
  • In Each Iteration we are moving downward so increment “y”
  • For each new Iteration we are starting from beginning but one co-ordinate back so decrement x
for(j=1;j<=i;j++)
         printf("%d ",i);
  • Nested Loop Used to Print All the Numbers in Particular Line . i.e in the third line i = 3 so j will print values 3 times .

Incoming search terms:

Related Articles:

9 Comments

  1. Anonymous September 5, 2010 at 7:59 am - Reply

    HOW TO PRINT THIS
    0
    1 1 1
    2 2 2 2 2
    3 3 3 3 3 3 3

    AND SO ON TILL 9.
    PLEASE TELL FAST I HAVE TO SUBMITT MY ASSIGNMENT
    THANKS

  2. Admin September 5, 2010 at 8:39 am - Reply

    void main()
    {
    int i,j;
    clrscr();

    for(i=0;i<=9;i++)
    {
    for( j=0 ; j<(1+2*i) ; j++ )
    printf("%d ",i);
    printf("n");
    }
    getch();
    }

    Here In the first row i=0 and total number of elements printed = 1+2i = 1

    Here In the Second row i=1 and total number of elements printed = 1+2i = 3

    Here In the Second row i=2 and total number of elements printed = 1+2i = 5


    ..
    ..
    ..
    ..
    ..
    So on

  3. ANJANA MURALIDHAR September 16, 2010 at 6:56 pm - Reply

    i wanted to know how to write a nested loop to create a pyramid pattern and get output. pls its urgent
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

  4. Admin September 17, 2010 at 4:21 am - Reply

    void main()
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("%dt",j);
    }
    printf("n");
    }
    getch();
    }

  5. Ansh October 11, 2010 at 11:07 am - Reply

    I wanted to know how to print a output if the input is given as
    2,4,3,6,1
    Output:
    *
    *
    * *
    ***
    ****
    *****

    and input is been changed.

  6. Admin October 13, 2010 at 5:58 am - Reply

    @Ansh If u r able to elaborate Problem Statement then it would be easy to answer

  7. shivam October 22, 2010 at 2:53 pm - Reply

    how to print
    ***
    ##
    $

  8. santo December 1, 2010 at 9:53 pm - Reply

    1
    12
    123
    1234
    12345

Leave A Response