The centred repeated number pyramid prints the row number as many times as the row is long, and centres each row: 1, then 2 2, then 3 3 3. It is a common first step from star pyramids to number pyramids.
Quick answer: Row i prints n − i spaces and then the number i, i times, each followed by a space. Since a number plus its space is two characters wide, one space less per row is enough to keep the pyramid centred.
This program is part of the Pyramid Patterns collection.
Table of contents8 sections
Centred Repeated Number Pyramid output
Output (n = 5):
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Same shape, other characters: the Alphabet Pyramid Pattern, Continuous Alphabet Pyramid, Dollar ($) Pyramid, Number Pyramid Pattern and Pascal’s Triangle programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the centred repeated number pyramid works
This is the full pyramid layout with digits. Each item on a row takes two characters, the digit and a space, so a row with i items is 2i characters wide. Moving one space less to the right each row keeps the middle of every row in the same column. The value is simply the row number, so the inner loop never looks at j.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n − ispaces. - Print
inumbers (valuei). - Move to the next line.
Key takeaways
- Leading spaces
n − icentre the rows. - Each item is two characters: the digit and its trailing space.
- The value is the row number, so the inner loop does not use
j. - Printing
jinstead ofiturns it into the number pyramid (1, 1 2, 1 2 3).
Centred Repeated Number Pyramid in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= i; j++)
printf("%d ", i);
printf("\n");
}
return 0;
}Centred Repeated Number Pyramid in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << i << " ";
cout << endl;
}
return 0;
}Centred Repeated Number Pyramid in Java
public class CentredRepeatedNumberPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(i + " ");
System.out.println();
}
}
}Centred Repeated Number Pyramid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, i + 1):
print(i, end=" ")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
Why do the rows stay centred?
Each row is two characters wider than the one above, one on each side. Starting each row one space further to the left uses up the extra character on the left side, so the centre never moves.
How do I print it without the spaces between numbers?
Drop the trailing space and print 2i − 1 copies, as in a star pyramid: 1, 222, 33333. The leading spaces stay n − i.
How do I turn it into a diamond?
After the loop, run a second loop from i = n − 1 down to 1 with the same spaces and numbers.






