Open In App
Related Articles

C Program to Print Pyramid Pattern

Improve Article
Improve
Save Article
Save
Like Article
Like

Pyramid pattern printing is a very popular logical program & It enhanced your logical thinking. In this article, we will learn how to make different types of pyramid patterns in c programming.

Pyramid Pattern

A pyramid is a 3 – dimensional shape that has a polygon base and the sides of the pyramid look like a triangle shape. Pyramid pattern printing is a logical program by which we can develop logical thinking in programming. We can use conditional statements and loop concepts for making pyramid patterns. Pyramid patterns can be printed using numbers, characters, and special characters like stars.

we will make 4 types of pyramid patterns.

  1. Half Pyramid of Numbers
  2. Inverted Half Pyramid of Numbers
  3. Full Pyramid of Numbers
  4. Full Pyramid of Numbers  in 180 Degree

1. Half Pyramid Pattern of Numbers

Half pyramid pattern of numbers looks like a right-angle triangle of numbers in which the hypotenuse is on the right side.

Examples: 

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

C Program to Print Half Pyramid Pattern of Numbers

C




// C program to print right half pyramid pattern of star
#include <stdio.h>
  
int main()
{
    int rows;
    printf("Number of rows: ");
    scanf("%d", &rows);
  
    // first loop for printing rows
    for (int i = 1; i <= rows; i++) {
  
        // second loop for printing similar number in each
        // rows
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}

Output : 

Number of rows: 5

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

2. Inverted Half Pyramid of Numbers

An inverted half-pyramid pattern of numbers is 180° reversed version of half pyramid pattern.

Examples: 

5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

C Program to Print Inverted Half Pyramid Pattern of Numbers

C




// C program to print inverted half pyramid pattern of number
#include <stdio.h>
    
int main()
{
    int rows;
      printf("Number of rows: ");
      scanf("%d", &rows);
    
    // first loop for printing rows
    for (int i = rows; i >= 1; i--) {
    
        // second loop for printing similar number in each rows
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}

Output : 

Number of rows: 5

5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

3. Full Pyramid Pattern of Numbers

A full pyramid pattern of numbers is similar to an equilateral triangle.

Examples: 

        1 
      2 2 2 
    3 3 3 3 3 
  4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5 

C Program to Print Full Pyramid Pattern of Numbers

C




// C program to print the full pyramid pattern of alphabets
#include <stdio.h>
  
int main()
{
    int rows;
    printf("Number of rows: ");
    scanf("%d", &rows);
  
    // first loop to print all rows
    for (int i = 1; i <= rows; i++) {
  
        // inner loop 1 to print white spaces
        for (int j = 1; j <= 2 * (rows - i); j++) {
            printf(" ");
        }
  
        // inner loop 2 to print numbers
        for (int k = 1; k < 2 * i; k++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}

Output : 

Number of rows: 5

        1 
      2 2 2 
    3 3 3 3 3 
  4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5 

4. Full Pyramid of Numbers  in 180 Degree

This pattern can be printed by combining half pyramid pattern and an inverted half-pyramid pattern.

Examples: 

        1
        22
        333
        4444
        55555
        4444
        333
        22
        1

C Program to Print Full Pyramid of Numbers  in 180 Degree

C




// C program to print the pyramid pattern
#include <stdio.h>
  
// Print the pattern upto n
void printPattern(int n)
{
    // Printing upper part
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            printf("%d", i);
        printf("\n");
    }
  
    // printing lower part
    for (int i = n - 1; i > 0; i--) {
        for (int j = i; j > 0; j--)
            printf("%d", i);
        printf("\n");
    }
}
  
// Driver Code
int main()
{
    int n = 8;
    printPattern(n);
    return 0;
}
  
// This code is contributed by bhartik021.

Output

1
22
333
4444
55555
666666
7777777
88888888
7777777
666666
55555
4444
333
22
1

Time complexity: O(n^2) // For given input n

Auxiliary Space: O(1)

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


Last Updated : 27 Mar, 2023
Like Article
Save Article
Similar Reads