In the world of C programming understanding and implementing patterns can be both educational and enjoyable. One such pattern is the pyramid pattern. In this article, we are going to write a C program to print a pyramid pattern of n numbers of rows.
Table of Contents
Logic Behind Pyramid Pattern
Before writing the code, Let’s understand the fundamental logic behind the printing pyramid pattern. The key concept revolves around nested loops- an inner loop for spaces and another for printing the pattern(*). The number of iterations in each loop determines the size and shape of a pyramid. The outer loop controls the number of rows(n) and the inner loop prints the spaces and symbols.
C Program for Pyramid Pattern
#include <stdio.h>
void main()
{
int n;
printf("Enter the value of n");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
// printing spaces
for (int space = 1; space <= n - i; space++)
{
printf(" ");
}
// printing *
for (int j = 1; j <= 2 * i - 1; j++)
{
printf("*");
}
printf("\n");
}
}
Output
Happy Coding
2 thoughts on “C Program to Print Pyramid Pattern”