Print a Full Pyramid Number Pattern in C

To print a full pyramid number pattern in C, we use nested loops: the outer loop controls the rows, while the inner loops manage spaces and numbers. The pyramid consists of centered numbers, increasing from the top to the bottom.


Examples of Full Pyramid Number Patterns

1. Basic Full Pyramid Number Pattern

In this example, we will print a simple full pyramid number pattern where numbers increase from left to right in each row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5; 

    for (int i = 1; i <= rows; i++) {
        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }
        // Print numbers
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("%d", k);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We define rows = 5 to set the number of pyramid levels.
  2. The outer loop for (int i = 1; i <= rows; i++) iterates over the rows. Refer C For Loop.
  3. The first inner loop prints spaces before the numbers to align them.
  4. The second inner loop prints numbers in an increasing sequence up to (2*i - 1) to form the pyramid.
  5. After each row, printf("\n") moves to the next line.

Output:

    1
   123
  12345
 1234567
123456789

2. Full Pyramid with Repeated Row Numbers

In this example, each row contains repeated numbers instead of increasing numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5; 

    for (int i = 1; i <= rows; i++) {
        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }
        // Print repeated row numbers
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We set rows = 5 to create five levels.
  2. The outer loop iterates from 1 to rows to print each row.
  3. The first inner loop prints spaces to center the numbers.
  4. The second inner loop prints the row number multiple times instead of increasing numbers.
  5. Each row ends with printf("\n") to move to the next level.

Output:

    1
   222
  33333
 4444444
555555555

3. Full Pyramid with Inverted Numbering

In this example, we print a pyramid where each row contains numbers in a descending sequence.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5; 

    for (int i = 1; i <= rows; i++) {
        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }
        // Print descending numbers
        for (int k = i; k >= 1; k--) {
            printf("%d", k);
        }
        for (int l = 2; l <= i; l++) {
            printf("%d", l);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We set rows = 5 to determine the height of the pyramid.
  2. The outer loop runs from 1 to rows.
  3. The first inner loop prints spaces for alignment.
  4. The second inner loop prints numbers in descending order from i to 1.
  5. The third inner loop prints numbers in ascending order from 2 to i.
  6. Each row is printed on a new line using printf("\n").

Output:

    1
   212
  32123
 4321234
543212345

Conclusion

In this tutorial, we covered different ways to print a full pyramid number pattern in C:

  1. Basic Full Pyramid: Increasing numbers in each row.
  2. Repeated Row Numbers: Each row consists of the same digit.
  3. Inverted Numbering: Numbers decrease to 1, then increase again.