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

Continuous Number Pyramid (1, 232, 34543)

The continuous number pyramid starts each row at the row number, climbs to its peak in the middle and comes back down: 1, then 232, then 34543. The peak goes up by two each row, so five rows reach 9.

Quick answer: Each row has three parts: n βˆ’ i spaces, then i numbers counting up from i (i + j βˆ’ 1), then i βˆ’ 1 numbers counting back down (2i βˆ’ 1 βˆ’ j). On row 4 that is 4 5 6 7 followed by 6 5 4.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Continuous Number Pyramid output
  2. How the continuous number pyramid works
  3. Continuous Number Pyramid in C
  4. Continuous Number Pyramid in C++
  5. Continuous Number Pyramid in Java
  6. Continuous Number Pyramid in Python
  7. Time and space complexity
  8. Frequently asked questions

Continuous Number Pyramid output

Output (n = 5):

    1
   232
  34543
 4567654
567898765
Continuous Number Pyramid layout diagram, row by row (n = 5)
How each row of the continuous number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Alphabet Palindrome Pyramid, Full Pyramid Star Pattern and Palindrome Number Pyramid 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 continuous number pyramid works

Every row is a palindrome built around its middle number, 2i βˆ’ 1. The rising half starts at i and needs i numbers to reach the middle. The falling half repeats those numbers in reverse, without the middle one, so it has i βˆ’ 1 numbers and starts at 2i βˆ’ 2. The spaces in front centre the row, exactly as in a star pyramid.

  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 + j βˆ’ 1).
  4. Print i βˆ’ 1 numbers (value 2 Γ— i βˆ’ 1 βˆ’ j).
  5. Move to the next line.

Key takeaways

  • Each row is a palindrome centred on 2i βˆ’ 1.
  • The rising half has i numbers, the falling half i βˆ’ 1.
  • Leading spaces n βˆ’ i centre the rows, as in the full pyramid.
  • Keep n at 5 or less if numbers are printed without spaces, so the peak stays a single digit.

Continuous 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 + j - 1);
        for (int j = 1; j <= i - 1; j++)
            printf("%d", 2 * i - 1 - j);
        printf("\n");
    }
    return 0;
}

Continuous 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 + j - 1;
        for (int j = 1; j <= i - 1; j++)
            cout << 2 * i - 1 - j;
        cout << endl;
    }
    return 0;
}

Continuous Number Pyramid in Java

public class ContinuousNumberPyramid {
    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 + j - 1);
            for (int j = 1; j <= i - 1; j++)
                System.out.print(2 * i - 1 - j);
            System.out.println();
        }
    }
}

Continuous 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 + j - 1, end="")
    for j in range(1, i):
        print(2 * i - 1 - j, 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

How is it different from the palindrome number pyramid?

The palindrome number pyramid starts every row at 1 (1, 121, 12321). Here each row starts at its own row number (1, 232, 34543), so the numbers climb higher.

What goes wrong for n = 6?

Row 6 peaks at 11, a two-digit number, and the row becomes wider than the rows above. Print every number with a space or a fixed width and double the leading spaces to keep the shape.

How do I check the formulas quickly?

Test row 3: i + j βˆ’ 1 gives 3, 4, 5 and 2i βˆ’ 1 βˆ’ j gives 4, 3. Put together that is 34543, which matches the output.

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 8 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.