New: 214 pattern programs with tested code in C, C++, Java and Python Explore patterns

Centred Repeated Number Pyramid (1, 2 2, 3 3 3)

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
  1. Centred Repeated Number Pyramid output
  2. How the centred repeated number pyramid works
  3. Centred Repeated Number Pyramid in C
  4. Centred Repeated Number Pyramid in C++
  5. Centred Repeated Number Pyramid in Java
  6. Centred Repeated Number Pyramid in Python
  7. Time and space complexity
  8. Frequently asked questions

Centred Repeated Number Pyramid output

Output (n = 5):

    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5
Centred Repeated Number Pyramid layout diagram, row by row (n = 5)
How each row of the centred repeated number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n − i spaces.
  3. Print i numbers (value i).
  4. Move to the next line.

Key takeaways

  • Leading spaces n − i centre 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 j instead of i turns 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 10 of 19 in Pyramid Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.