C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print Inverted Number Pyramid Pattern

Introduction

An Inverted Number Pyramid displays numbers in decreasing order of rows.
Each row starts with 1 and goes up to the current row limit, but the total number of rows decreases from n to 1.

For example, for n = 5:

                          1  2  3  4  5

                            1  2  3  4

                              1  2  3

                                1  2

                                  1

This pattern teaches reverse looping, space management, and nested loop structure.

 

C Program: Print Inverted Number Pyramid 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--) {

        // Print leading spaces

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

            printf(" ");

        }

 

        // Print numbers

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

            printf("%d ", k);

        }

 

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Explanation

  1. The outer loop runs from n down to 1.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to the current row limit (i).
  4. Each subsequent row prints one less number than the previous row.

 

C Program: Print Inverted Number Pyramid Pattern

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, k;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    i = n;

    while (i >= 1) {

        j = i;

        while (j < n) {

            printf(" ");

            j++;

        }

 

        k = 1;

        while (k <= i) {

            printf("%d ", k);

            k++;

        }

 

        printf("\n");

        i--;

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Explanation

  1. The outer loop runs from n down to 1.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to the current row limit (i).
  4. Each subsequent row prints one less number than the previous row.

 

C Program: Print Inverted Number Pyramid Pattern

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, k;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    i = n;

    do {

        j = i;

        do {

            if (j < n)

                printf(" ");

            j++;

        } while (j <= n);

 

        k = 1;

        do {

            if (k <= i)

                printf("%d ", k);

            k++;

        } while (k <= i);

 

        printf("\n");

        i--;

    } while (i >= 1);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Explanation

  1. The outer loop runs from n down to 1.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to the current row limit (i).
  4. Each subsequent row prints one less number than the previous row.