Numeric Pyramid Program in C With Explanation

Numeric Pyramid Program in C

In this tutorial, we will be going to learn numeric pyramid program in c. In c we can build various pyramids with numbers, alphabets or stars etc. We learn the number based pyramids. Using the numbers we can implement any type of pyramids some examples are as follow:

program – 1

Implement following numeric pyramid program in c

1

12

123

1234

12345

123456789101112131415161718192021222324252627#include<stdio.h>#include<conio.h>void main(){int i ,j;printf(“Number pyramid:\n”);for(i=1;i<=5;i++){for(j=1;j<=i;j++){printf(“%d”,j);}printf(“\n”);}}

OutPut :

Number pyramid:

1

12

123

1234

12345

In above program, as we are implementing the pyramid we require the number till which we want to print the pyramid that is we taken i variable to print number from 1 to 5.and for iterating loop we have taken the j variable with prints at each line of a pyramid.

program – 2

Implement following numeric pyramid program in c

12345

2345

345

45

5

123456789101112131415161718192021222324252627#include<stdio.h>#include<conio.h>void main(){int i ,j;printf(“Number pyramid:\n”);for(i=1;i<=5;i++){for(j=i;j<=5;j++){printf(“%d”,j);}printf(“\n”);}}

OutPut :

Number pyramid:

12345

2345

345

45

5

In above program, as we are implementing the pyramid we require the number till which we want to print the pyramid that is we taken j variable to print number from 5 to 1.and for iterating loop we have taken the j variable with prints at each line of a pyramid.

program – 3

Implement following numeric pyramid.

55555

45555

34555

23455

12345

123456789101112131415161718192021222324252627282930313233343536373839#include<stdio.h>#include<conio.h>void main(){int i ,j,k;for(i=5;i>=1;i–){k=i;for(j=1;j<=5;j++){if(k<=5){printf(“%d”,k);}else{printf(“5”);}k++;}print(“\n”);}}

Output :

Number pyramid:

55555

45555

34555

23455

12345

In above program, as we are implementing the pyramid as we see the first line contain only 5 so we take from 5 printing 5 times. then we require the variable iteration till which we want to print the pyramid that is we taken i variable. and after that change only number from left according to the iteration value.

program – 4

implement numeric diamond pyramid

      1

    121

  12321

1234321

 12321

  121

    1 

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#include<stdio.h>#include<conio.h>int main(){int num,r,c,sp;printf(“Enter number of rows :”);scanf(“%d”,&num);for(r=1;r<=num;r++){for(sp=num-r;sp>=1;sp–)printf(“ “);for(c=1;c<=r;c++)printf(“ %d“,c);for(c=r-1;c>=1;c–)printf(“ %d“,c);printf(“\n”);}for(r=1;r<=num;r++){for(sp=r;sp>=1;sp–)printf(“ “);for(c=1;c<=(num-r);c++)printf(“ %d“,c);for(c=num-r-1;c>=1;c–)printf(“ %d“,c);printf(“\n”);}getch();return 0;}

Output :

      1

    121

  12321

1234321

 12321

  121

    1

In above program, as we are implementing the pyramid we break the pyramid into the two parts as upper and lower so that task will be easy. In the first half, we printed the half portion as 1234321. and after that, we printed the last half.

program – 5

//implement following a numeric pyramid

1

23

4

56

7

89

10

12345678910111213141516171819202122232425262728293031323334353637383940414243#include<stdio.h>#include<conio.h>int main(){int r ,c,x ,y;static int i=1;for(r=1;r<=7;r++){if(r%2==0){for(x=2;x>=1;x–,i++)printf(“%d”,i);}else{for(y=1;y>=1;y–,i++)printf(“%d”,i);}printf(“\n”);}getch();return 0;}

Output :

1

23

4

56

7

89

10

In above program, as we are implementing the pyramid we require the number till we want to print pyramid we have taken the i variable for iterating the loop and the r for the number of to print the numbers as we iterate we increment the numbers and print.

program – 6

//implement following numeric pyramid.

            1

         1 2

      1 2 3

   1 2 3 4

1 2 3 4 5

1234567891011121314151617181920212223242526272829303132333435#include<stdio.h>#include<conio.h>int main(){int num,r,c,sp;printf(“Number pyramid:\n”);printf(“Enter number till you want pyramid:\n”);scanf(“%d”,&num);for(r=1;num>=r;r++){for(sp=num-r;sp>=1;sp–)printf(“ ”);for(c=1;c<=r;c++)printf(“%d”,c);printf(“\n”);}getch();return 0;}

OutPut :

Number pyramid:

Enter number till you want pyramid: 5

            1

         1 2

      1 2 3

   1 2 3 4

1 2 3 4 5

In above program, as we are implementing the pyramid we require the number till which we want to print the pyramid that is we taken in num variable to print number from 1 to 5.first we print spaces with num-1 and print the first number. after that, we put spaces with num-2 and print 2 numbers and so on till the end.