The Wayback Machine - https://web.archive.org/web/20240721172936/https://www.geeksforgeeks.org/c-program-print-pyramid-pattern/
Open In App

C Program to Print Pyramid Pattern

Last Updated : 29 Nov, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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

Prerequisites:

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 6 types of pyramid patterns here in this article.

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.

Pyramid Example: 

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.

Pyramid Example: 

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.

Pyramid Example: 

        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.

Pyramid Example: 

        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)

5. hollow pyramid of ” * ” & Numbers

This C program print the hollow pyramid means only the border part shows in the pyramid.

Pyramid Example:

    *
* *
* *
* *
* * * *

C Program to Print hollow pyramid of ” * ” & Numbers

C




#include <stdio.h>
 
int main() {
    int n = 5;
 
    for (int i = 1; i <= n; i++) {
        // Print leading spaces
        for (int j = i; j < n; j++) {
            printf(" ");
        }
 
        // Print asterisks
        for (int k = 1; k <= 2 * i - 1; k++) {
            if (k == 1 || k == 2 * i - 1 || i == n) {
                printf("*");
            } else {
                printf(" ");
            }
        }
 
        // Move to the next line after each row
        printf("\n");
    }
 
    return 0;
}


Output

    *
   * *
  *   *
 *     *
*********


6. Diamond Pyramid of ” * ” & Numbers

This C program print the Diamond Pyramid of ” * ” & Number. (for number just replace Stars (*) with their interator (I) in a loop.

    *
***
*****
*******
*********
*******
*****
***
*

C program to print Diamond Pyramid of ” * ” & Numbers

C




#include <stdio.h>
 
int main() {
    int n = 5;
 
    // Print the top pyramid
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (int k = 1; k <= 2 * i - 1; k++) {
            printf("*");
        }
        printf("\n");
    }
 
    // Print the inverted pyramid
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (int k = 1; k <= 2 * i - 1; k++) {
            printf("*");
        }
        printf("\n");
    }
 
    return 0;
}


Output

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *


FAQs on “print pyramid in c”

Q1: What is a pyramid pattern in C?

A pyramid pattern is a triangular pattern formed using characters, typically asterisks (*), printed in increasing or decreasing order.

Q2: How do you print a pyramid pattern in C?

To print a pyramid pattern in C, you can use nested for loops.

  • The outer loop controls the number of rows.
  • The inner loop controls the number of characters to print in each row.

You can use conditional statements to determine the increasing or decreasing pattern.

Q3: What are the different types of pyramid patterns in C?

There are various types of pyramid patterns in C, including:

  • Right-angled pyramid
  • Inverted pyramid
  • Centered pyramid
  • Hollow pyramid
  • Diamond pyramid


Previous Article
Next Article

Similar Reads

Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Program to Print Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *** ***** **************** Approach: The problem ca
4 min read
C Program To Print Character Pyramid Pattern
Here, we will build a C Program To Print Character Pyramid Pattern using 2 approaches i.e. Using for loopUsing while loop Input: rows = 5 Output: A B B C C C D D D D E E E E E 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern The first for loop is used to iterate the number of rows and the second for loop is
5 min read
C Program To Print Continuous Character Pyramid Pattern
There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows.The second for loop is used to repea
5 min read
Program to print a inverse pyramid character pattern
Given a positive integer n, print the inverse pyramid pattern upto n rows as shown in the examples.Examples : Input : 4 Output : A B C D D C B A A B C C B A A B B A A A Input : 6 Output : A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A Below is the implementation for the pattern: C/C++ Code // C++ code to print i
6 min read
Program to print pyramid pattern
Write to program to print the pyramid pattern formed of stars Example : Input: n = 6 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * We strongly recommend you to minimize your browser and try this yourself first.The idea is to use two for loops for every part of the pyramid. The two parts may be classified as upper p
5 min read
three90RightbarBannerImg