Write a C Program to Print a Pyramid Pattern of stars, numbers, and alphabets using a while loop, for loop, do while loop, and functions with an example. It also shows you how to print a Pyramid Pattern (or Equilateral Triangle) with different symbols.
C Program to Print Star Pyramid Pattern using While Loop
This program allows the user to enter the maximum number of rows the user wants to print. Here, we will print the Pyramid of * symbols until it reaches the user-specified rows.
#include <stdio.h> int main() { int Rows, i, j, k = 0; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); printf("Printing Star Pyramid Pattern \n \n"); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } while (k != (2 * i - 1)) { printf("*"); k++; } k = 0; printf("\n"); } return 0; }
Let us see the Nested for
Outer Loop – First Iteration of C Program to Print Star Pyramid Pattern.
From the above C Programming screenshot, you can observe that the value of i is 1 and Rows is 10, so the condition (i <= 10) is True. So, it will enter into the second for loop.
Inner For Loop – First Iteration: The j value is 1, and the condition (j <= 9) is True. So, it will start executing the statements inside the loop. So, it will start executing the printf(” “) statement until the condition fails.
Inner While Loop – First Iteration: The k value is 0, and the condition k != 2*i – 1 (0 != 1) is True. So, it will start executing the statements inside it. So, it will start executing printf(“*”) statements until the condition fails.
Next, the iteration will start from the beginning until both the Inner and Outer loop conditions fail.
C Program to Print Pyramid Pattern of stars using For Loop
In this C program, we just replaced the While Loop with the For Loop. I suggest you refer to the For Loop article to understand the logic.
#include <stdio.h> int main() { int Rows, i, j, k; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } for (k = 1; k <= (2 * i - 1); k++) { printf("*"); } printf("\n"); } return 0; }
Please Enter the Number of Rows: 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
This program uses the do while loop and prints the pyramid pattern of stars.
#include <stdio.h> int main(void) { int Rows, i, j, k; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); i = 1 ; do { j = 0 ; do { printf(" "); j++; }while ( j <= Rows-i); k = 0; do { printf("*"); k++; }while (k != (2 * i - 1)); printf("\n"); i++; }while ( i <= Rows ); }
Output.
Please Enter the Number of Rows: 13
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
This C program allows users to enter the Symbol and the number of rows they want to print. Instead of printing pre-defined stars, the user can enter their own character.
#include <stdio.h> int main() { int Rows, i, j, k = 0; char ch; printf("Please Enter any Symbol : "); scanf("%c", &ch); printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } while (k != (2 * i - 1)) { printf("%c", ch); k++; } k = 0; printf("\n"); } return 0; }
Please Enter any Symbol : $
Please Enter the Number of Rows: 12
$
$$$
$$$$$
$$$$$$$
$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$
Hollow Pyramid Pattern of Stars
This program prints the hollow pyramid of stars. For more examples >> Click Here.
#include<stdio.h> int main(void) { int i, j, rows; printf("Enter Hollow Star Pyramid Rows = "); scanf("%d", &rows); for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { printf(" "); } if(i == 1 || i == rows) { for(j = 1; j <= i * 2 - 1; j++) { printf("*"); } } else { for(j = 1; j <= i * 2 - 1; j++) { if(j == 1 || j == i * 2 - 1) { printf("*"); } else { printf(" "); } } } printf("\n"); } }
Output
Enter Hollow Star Pyramid Rows = 9
*
* *
* *
* *
* *
* *
* *
* *
*****************
Inverted Pyramid Pattern of Stars
This program prints the inverted pyramid of stars. For more examples >> Click Here.
#include<stdio.h> int main(void) { int rows; printf("Enter the Number of Rows: "); scanf("%d", &rows); for (int i = rows ; i >= 1; i-- ) { for (int j = 0 ; j <= rows-i; j++ ) { printf(" "); } for (int k = 0; k < (2 * i - 1); k++) { printf("*"); } printf("\n"); } return 0; }
Output
Enter the Number of Rows: 12
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Hollow Inverted Pyramid Pattern of Stars
This program prints the hollow inverted pyramid of stars. For more examples >> Click Here.
#include<stdio.h> int main(void) { int i, j, rows; printf("Enter Hollow Inverted Star Pyramid Rows = "); scanf("%d", &rows); for(i = rows; i > 0; i--) { for(j = 1; j <= rows - i; j++) { printf(" "); } if(i == 1 || i == rows) { for(j = 1; j <= i * 2 - 1; j++) { printf("*"); } } else { for(j = 1; j <= i * 2 - 1; j++) { if(j == 1 || j == i * 2 - 1) { printf("*"); } else { printf(" "); } } } printf("\n"); } }
Output
Enter Hollow Inverted Star Pyramid Rows = 9
*****************
* *
* *
* *
* *
* *
* *
* *
*
C Program to Print Pyramid Numbers Pattern
This program prints the pyramid of numbers. For more examples >> Click Here.
#include<stdio.h> int main(void) { int rows; printf("Enter Pyramid Number Pattern Rows = "); scanf("%d", &rows); for (int i = 1; i <= rows; i++) { for (int j = rows; j > i; j--) { printf(" "); } for (int k = 1; k <= i; k++) { printf("%d ", i); } printf("\n"); } }
Output
Enter Pyramid Number Pattern Rows = 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
C Program to Print Pyramid Pattern of Alphabets
This program prints the pyramid pattern of the alphabet. For more examples >> Click Here.
#include <stdio.h> int main(void) { int rows, alphabet = 64; printf("Enter Pyramid of Alphabets Rows = "); scanf("%d", &rows); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= rows - i; j++) { printf(" "); } for (int k = i; k > 0; k--) { printf("%c", alphabet + k); } for (int l = 2; l <= i; l++) { printf("%c", alphabet + l); } printf("\n"); } }
Output
Enter Pyramid of Alphabets Rows = 12
A
BAB
CBABC
DCBABCD
EDCBABCDE
FEDCBABCDEF
GFEDCBABCDEFG
HGFEDCBABCDEFGH
IHGFEDCBABCDEFGHI
JIHGFEDCBABCDEFGHIJ
KJIHGFEDCBABCDEFGHIJK
LKJIHGFEDCBABCDEFGHIJKL