0
0
CProgramBeginner · 2 min read

C Program to Print Pyramid Pattern

You can print a pyramid pattern in C using nested for loops where the outer loop controls the rows and the inner loops print spaces and stars; for example: for(i=1; i<=n; i++){ for(j=1; j<=n-i; j++) printf(" "); for(k=1; k<=2*i-1; k++) printf("*"); printf("\n"); }.
📋

Examples

Input3
Output * *** *****
Input5
Output * *** ***** ******* *********
Input1
Output*
🧠

How to Think About It

To print a pyramid pattern, think of each row having spaces followed by stars. The number of spaces decreases each row, and the number of stars increases by two. Use one loop for rows, one loop to print spaces, and one loop to print stars.
📐

Algorithm

1
Get the number of rows (n) from the user
2
For each row from 1 to n:
3
Print (n - current row) spaces
4
Print (2 * current row - 1) stars
5
Move to the next line
💻

Code

c
#include <stdio.h>

int main() {
    int n, i, j, k;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {
        for(j = 1; j <= n - i; j++)
            printf(" ");
        for(k = 1; k <= 2 * i - 1; k++)
            printf("*");
        printf("\n");
    }
    return 0;
}
Output
Enter number of rows: 5 * *** ***** ******* *********
🔍

Dry Run

Let's trace the program for input n=3 to see how spaces and stars print each row.

1

Row 1

Print 2 spaces and 1 star: ' *'

2

Row 2

Print 1 space and 3 stars: ' ***'

3

Row 3

Print 0 spaces and 5 stars: '*****'

RowSpacesStarsOutput
121 *
213 ***
305*****
💡

Why This Works

Step 1: Outer loop controls rows

The for loop with variable i runs from 1 to n, controlling how many rows the pyramid has.

Step 2: Print spaces before stars

For each row, print n - i spaces to align stars in the center forming the pyramid shape.

Step 3: Print stars increasing by two

Print 2 * i - 1 stars for each row, increasing the count by two to form the pyramid's width.

🔄

Alternative Approaches

Using while loops
c
#include <stdio.h>

int main() {
    int n, i = 1, j, k;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    while(i <= n) {
        j = 1;
        while(j <= n - i) {
            printf(" ");
            j++;
        }
        k = 1;
        while(k <= 2 * i - 1) {
            printf("*");
            k++;
        }
        printf("\n");
        i++;
    }
    return 0;
}
This uses while loops instead of for loops; it is more verbose but shows loop control explicitly.
Using recursion
c
#include <stdio.h>

void printPyramid(int n, int row) {
    if(row > n) return;
    for(int i = 1; i <= n - row; i++) printf(" ");
    for(int i = 1; i <= 2 * row - 1; i++) printf("*");
    printf("\n");
    printPyramid(n, row + 1);
}

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);
    printPyramid(n, 1);
    return 0;
}
This recursive approach calls the function for each row, demonstrating a different control flow style.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and inner loops run up to n times, resulting in O(n^2) time.

Space Complexity

Only a few integer variables are used; no extra space proportional to input size, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; iterative for-loop method is simplest and most readable.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simplicity and readability
While loopsO(n^2)O(1)Explicit loop control
RecursionO(n^2)O(n) due to call stackDemonstrating recursion
💡
Use nested loops: one for spaces and one for stars to align the pyramid properly.
⚠️
Beginners often forget to print the correct number of spaces, causing the pyramid to be misaligned.