C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print Inverted Half Pyramid (Reversed Star Pattern)

Introduction

The Inverted Half Pyramid (or reversed right-angled triangle) pattern is a simple yet classic program that helps understand nested loops.
It prints stars (*) in decreasing order — starting with the maximum number in the first row and reducing one star per row.

 

C Program: Print Inverted Half Pyramid (Reversed Star Pattern)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int n;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    for (int i = n; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {

            printf("*");

        }

        printf("\n");

    }

 

    return 0;

}

Output

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



Explanation

  • The outer for loop runs from n to 1, decreasing by 1 each time.
  • The inner for loop prints stars (*) equal to the current row number i.
  • The result is an inverted triangle pattern.

 

C Program: Print Inverted Half Pyramid (Reversed Star Pattern)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 0, j;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    i = n;

    while (i >= 1) {

        j = 1;

        while (j <= i) {

            printf("*");

            j++;

        }

        printf("\n");

        i--;

    }

 

    return 0;

}

Output

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

Explanation

  • The outer while loop starts from n and decrements i until 1.
  • The inner loop prints stars corresponding to each row.
  • It continues until all rows are printed in decreasing order.

 

C Program: Print Inverted Half Pyramid (Reversed Star Pattern)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1, j;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    i = n;

    do {

        j = 1;

        do {

            printf("*");

            j++;

        } while (j <= i);

 

        printf("\n");

        i--;

    } while (i >= 1);

 

    return 0;

}

Output

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

Explanation

  • The outer ..while loop runs for each row.
  • The inner loop prints stars according to the row count.
  • Since ..while ensures execution at least once, it correctly handles cases like n = 1.