C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print inverted pyramid pattern

Introduction

An inverted pyramid pattern displays stars (*) in a descending order, starting with the maximum number of stars in the first row and reducing them by two in each subsequent row.

This program uses nested for loops to print the pattern.

 

C Program: Print inverted pyramid pattern

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 = n; i >= 1; 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 user inputs the number of rows (n).
  2. The outer for loop starts from n and decrements to 1.
  3. The first inner loop prints spaces before stars.
  4. The second inner loop prints (2 * i - 1)
  5. Each iteration reduces the number of stars by two, forming an inverted pyramid.

 

C Program: Print inverted pyramid pattern

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, space;

 

    // Input number of rows

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    // Initialize row counter

    i = n;

 

    // Outer loop for rows

    while (i >= 1) {

        space = i;

        j = 1;

 

        // Print spaces

        while (space < n) {

            printf(" ");

            space++;

        }

 

        // Print stars

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

            printf("*");

            j++;

        }

 

        // Move to next line

        printf("\n");

 

        // Decrement row

        i--;

    }

 

    return 0;

}

Output

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

Explanation

  1. The user inputs the number of rows (n).
  2. The outer while loop starts from i = n and decreases to 1.
  3. The first inner loop prints spaces before stars.
  4. The second inner loop prints stars using the formula (2 * i - 1).
  5. Each iteration prints one full row, reducing the number of stars by two each time.

 

C Program: Print inverted pyramid pattern

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, space;

 

    // Input number of rows

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    // Initialize row counter

    i = 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");

 

        // Decrement row

        i--;

    } while (i >= 1);

 

    return 0;

}

Output

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

Explanation

  1. The program takes user input for the number of rows (n).
  2. The outer ..while loop runs from i = n to 1.
  3. The first inner ..while prints spaces before stars.
  4. The second inner ..while prints (2 * i - 1) stars.
  5. Each iteration prints one row and decreases the star count by two.