C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print pyramid pattern (star *)

Introduction

A pyramid star pattern is a common exercise to practice nested loops in C.
This program uses two for loops:

  • The outer loop controls the number of rows.
  • The inner loops print spaces and stars to form a pyramid shape.

 

C Program: Print pyramid pattern (star *)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, space;

 

    // Input number of rows

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    // Outer loop for rows

    for (i = 1; i <= n; i++) {

        // Print leading spaces

        for (space = i; space < n; space++) {

            printf(" ");

        }

 

        // Print stars

        for (j = 1; j <= (2 * i - 1); j++) {

            printf("*");

        }

 

        // Move to next line

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    *
   ***
  *****
 *******
*********


Explanation

  1. The program takes the number of rows (n) as input.
  2. The outer loop (i) runs from 1 to n (rows).
  3. The first inner loop prints spaces to align stars properly.
  4. The second inner loop prints stars (*) in each row — formula: 2 * i - 1.
  5. After printing each row, a newline (\n) moves the cursor to the next line.

C Program: Print pyramid pattern (star *)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1, j, space;

 

    // Input number of rows

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    // Outer loop for rows

    while (i <= n) {

        space = i;

        j = 1;

 

        // Print leading spaces

        while (space < n) {

            printf(" ");

            space++;

        }

 

        // Print stars

        while (j <= (2 * i - 1)) {

            printf("*");

            j++;

        }

 

        // Move to next line

        printf("\n");

 

        // Increment row counter

        i++;

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    *
   ***
  *****
 *******
*********


Explanation

  1. The user enters the number of rows (n).
  2. The outer while loop runs from i = 1 to n (rows).
  3. The first inner while loop prints spaces before stars.
  4. The second inner while loop prints (2 * i - 1) stars for each row.
  5. Each iteration prints one complete row, then moves to the next.

 

C Program: Print pyramid pattern (star *)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1, j, space;

 

    // Input number of rows

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    // Outer loop for rows

    do {

        space = i;

        j = 1;

 

        // Print spaces

        do {

            if (space >= n)

                break;

            printf(" ");

            space++;

        } while (1);

 

        // Print stars

        do {

            if (j > (2 * i - 1))

                break;

            printf("*");

            j++;

        } while (1);

 

        // Move to next line

        printf("\n");

 

        i++;

    } while (i <= n);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    *
   ***
  *****
 *******
*********


Explanation

  1. The user inputs the number of rows (n).
  2. The outer ..while loop ensures at least one row prints.
  3. The first inner ..while prints spaces to align stars in the center.
  4. The second inner ..while prints (2 * i - 1) stars.
  5. Each iteration prints one row, then increments the row counter (i).