C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print Number Pyramid Pattern

Introduction

A Number Pyramid Pattern is similar to the star pyramid, but instead of stars (*), we print numbers.
Each row contains numbers from 1 up to the row number, arranged in a centered format.

For example, for n = 5:

                                  1

                               1    2

                             1   2    3

                            1  2   3   4

                          1  2   3   4   5

This program demonstrates nested loops, pattern alignment, and number printing in a triangular format.

 

C Program: Print 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 = 1; i <= n; 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
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5

Explanation

  1. The outer loop controls the rows.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to i.
  4. Each row increases by one number.

 

C Program: Print Number Pyramid Pattern

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1, j, k;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    while (i <= n) {

        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
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5

Explanation

  1. The outer loop controls the rows.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to i.
  4. Each row increases by one number.

 

C Program: Print Number Pyramid Pattern

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 1, j, k;

    printf("Enter number of rows: ");

    scanf("%d", &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 <= n);

 

    return 0;

}

Output

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

Explanation

  1. The outer loop controls the rows.
  2. The first inner loop prints spaces for alignment.
  3. The second inner loop prints numbers from 1 to i.
  4. Each row increases by one number.