Tutorials Tonight
Follow Us

C Program To Print Pyramid Pattern Of Numbers


In this article, you will look at C program to print pyramid pattern of numbers. We will see different types of pyramids like hollow pyramids, reverse pyramids, etc.

C program to print pyramid pattern of numbers

    Table Of Contents

  1. Number pyramid pattern in C
    1. Pyramid - Number increment in row
    2. Pyramid - Number increment in column
  2. Reverse Number pyramid pattern in C
    1. Reverse pyramid - Number increment in row
    2. Reverse pyramid - Number increment in column
  3. Hollow Number pyramid
  4. Hollow reverse pyramid

1. Number pyramid pattern in C

Number pyramid pattern in C is a very famous pyramid pattern made up of numbers and has the shape of an equilateral triangle which is called a pyramid in this sense.

These patterns can be created with different kinds of arrangements of numbers. We will see 2 types here. In one types number increase in a row and in other types number increase in the column.

1.1. Pyramid - Number increment in row

In this pattern, the number starts from 1 in each row and increases throughout the row. And from next line again starts from 1. Here is how it looks.

    1
   123
  12345
 1234567
123456789

Here are the steps to create this pattern.

  1. Take variable 'size' to store the size of the pyramid.
  2. Set it a value either by user input or default value.
  3. Create a for loop to print pyramid and execute it for 'size' number of times.
  4. Inside this loop create 2 other loops first one to print spaces and the second one to print numbers.
  5. Print spaces for size of pyramid minus current row number.
  6. Print numbers for 2 * current row number + 1. Use row variable to print increasing numbers.
  7. Print new line after each row.
#include <stdio.h>

int main() {
  // size of pyramid
  // can also be read from stdin
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < size-i-1; j++) {
      printf(" ");
    }
    // printing number
    for (int k = 0; k < 2*i+1; k++) {
      printf("%d", k+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

    1
   123
  12345
 1234567
123456789

1.2. Pyramid - Number increment in column

In this pattern number starts from 1 in the first row, in second-row number will be 2 and the same numbers will be printed throughout the row. Here is how it looks.

    1
   222
  33333
 4444444
555555555

Steps to create this are almost the same as above but in step number 6 use the external variable 'i' to print numbers. This will cause printing the same number throughout the row and number increase only in the next row.

#include <stdio.h>

int main() {
  // size of pyramid
  // can also be read from stdin
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < size-i-1; j++) {
      printf(" ");
    }
    // printing numbers
    for (int k = 0; k < 2*i+1; k++) {
      printf("%d", i+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

    1
   222
  33333
 4444444
555555555

2. Reverse Number pyramid pattern in C

The reverse number pyramid is a pyramid pattern just rotated 180 degrees. You can also visualize it as a water image of a number pyramid.

Again it can have 2 types. One type has an increasing number in a row and another type has an increasing number in the column.

2.1. Reverse pyramid - Number increment in row

This is a reverse number pyramid pattern where numbers start from 1 in each row and increase throughout the row. Here is how it looks.

123456789
 1234567
  12345
   123
    1

The steps to create a reverse number pyramid is as follows:

  1. Take the size of your pyramid.
  2. Create a nested loop to print the pyramid.
  3. In the first internal loop print spaces for current row number minus 1.
  4. In the second internal loop print numbers for 2 times of size minus current row number and minus 1.
  5. Print newline after end of the second internal loop.
#include <stdio.h>

int main() {
  // size of pyramid
  // can also take user input
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // print number
    for (int k = 0; k < 2*(size-i)-1; k++) {
      printf("%d", k+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

123456789
 1234567
  12345
   123
    1

2.2. Reverse pyramid - Number increment in column

This is a reverse number pyramid pattern and numbers start from 1 in the first row, 2 in the second row and so on. Here is how it looks.

111111111
 2222222
  33333
   444
    5

This is the same as the above program just change the number printing variable to 'i' and you will have this pattern.

#include <stdio.h>

int main() {
  // size of pyramid
  // can also take user input
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // print number
    for (int k = 0; k < 2*(size-i)-1; k++) {
      printf("%d", i+1);
    }
    printf("\n");
  }
  return 0;
}

Output:

111111111
 2222222
  33333
   444
    5

3. Hollow number pyramid pattern

The hollow number pyramid pattern is a pyramid pattern that has a number only at boundaries and is hollow inside.

    1
   1 2
  1   2
 1     2
123456789

The complete code for the hollow number pyramid pattern program in C is as follows:

#include <stdio.h>

int main() {
  // size of pyramid
  // can also take user input
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    for (int j = 0; j < size-i-1; j++) {
      printf(" ");
    }
    // printing number and inner spaces
    int num = 1;
    for (int k = 0; k < 2*i+1; k++) {
      if (i == 0 || i == size - 1) {
        printf("%d", num++);
      } else {
        if (k == 0 || k == 2*i) {
          printf("%d", num++);
        } else {
          printf(" ");
        }
      }
    }
    printf("\n");
  }
  return 0;
}

Output:

    1
   1 2
  1   2
 1     2
123456789

4. Hollow reverse number pyramid pattern

The hollow reverse number pyramid pattern is a pyramid pattern that is reversed as well as hollo inside.

123456789
 1     2
  1   2
   1 2
    1

The code of hollow reversed number pyramid is very much like a normal hollow pyramid pattern, with just a little change in the code.

#include <stdio.h>

int main() {
  // size of pyramid
  // can also take user input
  int size = 5;
  for (int i = 0; i < size; i++) {
    // printing spaces
    // in order 1, 2 , 3,...
    for (int j = 0; j < i; j++) {
      printf(" ");
    }
    // printing number and inner spaces
    int num = 1;
    for (int k = 0; k < 2*(size-i)-1; k++) {
      // print number in 1st and last row
      if (i == 0 || i == size - 1) {
        printf("%d", num++);
      } else {
        // print number at 1st and last position of other rows
        if (k == 0 || k == 2*(size-i-1)) {
          printf("%d", num++);
        } else {
          printf(" ");
        }
      }
    }
    printf("\n");
  }
  return 0;
}

Output:

123456789
 1     2
  1   2
   1 2
    1

Conclusion

In this short guide, we have seen 6 different C program to print pyramid pattern of numbers. Here is the list of other pattern programs in C that you must have a look at.

If you want to check more pattern programs in another programming language than C then look at star pattern in javascript, pattern program in python, star pattern program in PHP, and pattern program in java.