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

Stepped Number Triangle (1, 23, 345, 4567)

In the stepped number triangle every row starts at its own row number and counts up: 1, 23, 345, 4567, 56789. The first column steps down 1 to 5 and the last column steps down in odd numbers.

Quick answer: Row i prints i numbers starting at i, so the value is i + j − 1. On row 4 that gives 4, 5, 6, 7. The last number on each row is 2i − 1: 1, 3, 5, 7, 9.

This program is part of the Number Patterns collection.

Table of contents8 sections
  1. Stepped Number Triangle output
  2. How the stepped number triangle works
  3. Stepped Number Triangle in C
  4. Stepped Number Triangle in C++
  5. Stepped Number Triangle in Java
  6. Stepped Number Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Stepped Number Triangle output

Output (n = 5):

1
23
345
4567
56789
Stepped Number Triangle layout diagram, row by row (n = 5)
How each row of the stepped number triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Alternating Star and Hash Triangle, Descending Number Triangle, Descending Repeated Number Triangle, Number Right Triangle and Repeated Number 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 stepped number triangle works

Both loop counters are used in the value. i sets where the row starts and j walks along it. That is the same expression used in the rising half of the continuous number pyramid, here printed on its own and left-aligned, which makes it a good stepping stone to that pattern.

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

Key takeaways

  • The value i + j − 1 starts at the row number and climbs by one.
  • The last number of each row is 2i − 1, an odd number.
  • The first column is 1 to n and the last column is 1, 3, 5, 7, 9.
  • Keep n at 5 so that the largest number, 9, still has one digit.

Stepped Number Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            printf("%d", i + j - 1);
        printf("\n");
    }
    return 0;
}

Stepped Number Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << i + j - 1;
        cout << endl;
    }
    return 0;
}

Stepped Number Triangle in Java

public class SteppedNumberTriangle {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(i + j - 1);
            System.out.println();
        }
    }
}

Stepped Number Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(i + j - 1, 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

What breaks for n = 6?

Row 6 runs from 6 to 11, so it contains 10 and 11, which have two digits. Add a space after each number to keep the columns readable.

How do I continue the numbers instead of restarting each row?

Use a single counter that never resets, as in Floyd’s triangle: 1, 23, 456, 78910.

Can I mirror this triangle?

Yes. Print n − i spaces first. The rows then end in the same column and the triangle points to the left.

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 23 of 24 in Number 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.