pyramid triangle

C program to pyramid number pattern

C program to pyramid number pattern

In this tutorial, we will learn about C program to pyramid number pattern.

We can display various pyramid pattern using nested for loop in C programming  language

pyramid number pattern in C language

Code to Pyramid pattern 1

C code to Pyramid pattern 1

Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k=9,m;
    for(i=1; i<=9; i++){
        for(j=1; j<=k; j++){
                printf(" ");
        }
        for(m=1; m<=i; m++){
                printf("%d",i);
                printf(" ");
        }
        printf("\n");

        k=k-1;
    }
        getch();
    return 0;
}

When the above code is executed, it produces the following results:

        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
   

this Pyramid is made of numers

Code to Pyramid pattern 2

C code to Pyramid pattern 2

Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows,i,j,k,l=1;
    printf("Enter number of rows you want: ");
    scanf("%d",&rows);
    printf("Pyramid pattern 1\n");
    for(i=1; i<=rows; i++){
        for(j=1; j<=rows-i; j++){
            printf(" ");
        }
        for(k=1; k<=i; k++, l++){
            printf("%d ",l);
        }
        printf("\n");
    }
    return 0;
}

When the above code is executed, it produces the following results:

  Enter number of rows you want : 9
Pyramid pattern 1
               1
              2 3
             4 5 6
           7 8 9 10
       11 12 13 14 15
      16 17 18 19 20 21
    22 23 24 25 26 27 28
  29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45

 

Code to Pyramid pattern 3

 pascal’s triangle piramid pattern

C code to Pyramid pattern 3

Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows,i,j,space_count,count=1;
    printf("Enter the number of rows you want:");
    scanf("%d",&rows);
    for(i=0; i<=rows; i++){//outer for loop - parent for
        for(space_count=1; space_count<=rows-i; space_count++ ){
            printf(" "); //print space, inner for loop(child)
        }
        for(j=0; j<=i; j++)
        {
            if(j==0 || i==0)
                count=1;
            else
                count=count*(i-j+1)/j;
            printf("%4d",count);

        }
        printf("\n");

    }
    printf("Pascal's pyramid pattern\n");
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

   Enter the number of rows you want:7
                   1
                1    1
             1    2    1
          1    3    3    1
      1    4    6     4    1
   1    5    10   10  5    1
1    6   15   20  15   6    1

 

Code to Pyramid pattern 4

C code to Pyramid pattern 4
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j=0,rows,num=0,count=0,spaceCount;
    printf("Enter the number of rows you want");
    scanf("%d",&rows);
    for(i=1; i<=rows; ++i){//outer for loop(parent)
        for(spaceCount=1; spaceCount<=rows-i; spaceCount++){
            printf(" ");//print space for Pyramid pattern
            count++;
        }
        while(j!=2*i-1)
        {
            if(count<=rows-1)
            {
                printf("%d", i+j);
                num++;
            }
            else{
                num++;
                printf("%d",(i+j-2*num));
            }
            ++j;
        }
        num=count=j=0;
    printf("\n");
}
    return 0;
}

When the above code is executed, it produces the following results:

Enter the number of rows you want:5
                                           1
                                        2 3 4
                                     3 4 5 6 7
                                  4 5 6 7 89 10
                               5 6 7 8 9 10 11 12 13

 

 

Code to Pyramid pattern 5

C code to Pyramid pattern 5

Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j=0,rows,num=0,count=0,spaceCount;
    printf("Enter the number of rows you want:");
    scanf("%d",&rows);
    for(i=1; i<=rows; ++i){//outer for loop(parent)
        for(spaceCount=1; spaceCount<=rows-i; spaceCount++){
            printf(" ");//print space for Pyramid pattern
            count++;
        }
        while(j!=2*i-1)
        {
            if(count<=rows-1)
            {
                printf("%d", i+j);
                ++count;
            }
            else{
                num++;
                printf("%d",(i+j-2*num));
            }
            ++j;
        }
        num=count=j=0;
    printf("\n");
}
    return 0;
}

 

Enter the number of rows you want:5
                                           1
                                        2 3 2
                                     3 4 5 4 3
                                  4 5 6 7 6 5 4
                               5 6 7 8 9 8 7 6 5

Code to Pyramid pattern 6

C code to Pyramid pattern 6

Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,rows;
    printf("Enter number of rows you want: ");
    scanf("%d",&rows);
    printf("Pyramid pattern 1\n");
    for (i=1; i<=rows; i++){
      for (j=1; j<=rows-i; j++){
      printf(" ");
  }//print space at the beginning of each row

for(j=1; j<i; j++) {
printf("%d ",j);//print left part of pyramid
}
  printf("\n");
}
getch();
    return 0;
}

 

              1
            1  2
          1  2  3 
        1  2  3  4
      1  2  3  4  5
    1  2  3  4  5  6
  1  2  3  4  5  6  7
1  2  3  4  5  6  7  8

 

Suggested for you

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

 

Java code to reverse order triangle number patterns
Java program to display star Pyramid pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

3 weeks ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

4 weeks ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

1 month ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago