Print a Half Pyramid Number Pattern in C

To print a half pyramid number pattern in C, we use nested loops. The outer loop controls the rows, while the inner loop prints numbers in increasing order. Each row contains numbers from 1 up to the row index, forming a triangular pattern.


Examples of Printing Half Pyramid Number Pattern

1. Printing a Simple Half Pyramid Number Pattern

In this example, we will print a half pyramid pattern using numbers. The rows will contain increasing numbers, starting from 1 in the first row and adding one more number in each subsequent row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop for numbers
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n"); // Move to next line after each row
    }

    return 0;
}

Explanation:

  1. We declare an integer variable rows to define the number of rows in the pyramid.
  2. The outer loop (for (int i = 1; i <= rows; i++)) controls the number of rows in the pattern.
  3. The inner loop (for (int j = 1; j <= i; j++)) prints numbers from 1 to the row index i. Refer C For Loop.
  4. The printf("%d ", j) statement prints the numbers with a space.
  5. After printing each row, printf("\n") moves to the next line.

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

2. Printing a Half Pyramid with Repeated Row Numbers

In this example, instead of printing numbers from 1 to the row index, we will print the row index itself multiple times.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop for numbers
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n"); // Move to next line after each row
    }

    return 0;
}

Explanation:

  1. We declare rows = 5 to define the number of rows.
  2. The outer loop (for (int i = 1; i <= rows; i++)) controls the number of rows.
  3. The inner loop (for (int j = 1; j <= i; j++)) repeats i for each column.
  4. The printf("%d ", i) statement prints the current row number.
  5. The printf("\n") statement moves to the next line.

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

3. Printing an Inverted Half Pyramid of Numbers

In this example, we will print an inverted half pyramid, where the highest row number appears first, decreasing row by row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = rows; i >= 1; i--) {
        // Inner loop for numbers
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n"); // Move to next line after each row
    }

    return 0;
}

Explanation:

  1. We declare rows = 5 to define the number of rows.
  2. The outer loop starts from rows and decrements down to 1.
  3. The inner loop prints numbers from 1 up to the current row number i.
  4. The printf("%d ", j) statement prints the numbers.
  5. The printf("\n") moves to the next line.

Output:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Conclusion

In this tutorial, we covered how to print half pyramid number patterns using nested loops:

  1. A simple half pyramid where numbers increase from 1.
  2. A variation where each row contains repeated numbers.
  3. An inverted half pyramid where rows decrease in size.