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
Stepped Number Triangle output
Output (n = 5):
1 23 345 4567 56789

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
inumbers (valuei + j − 1). - Move to the next line.
Key takeaways
- The value
i + j − 1starts 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
nand the last column is 1, 3, 5, 7, 9. - Keep
nat 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.






