The suffix number triangle prints 12345 and then removes the first number on each row: 2345, 345, 45, 5. Every row is a suffix, or ending, of the first one.
Quick answer: Row i prints n β i + 1 numbers starting from i, so the value at position j is i + j β 1. Every row ends at n, and the left-aligned rows make the numbers drift to the left.
This program is part of the Number Patterns collection.
Table of contents8 sections
Suffix Number Triangle output
Output (n = 5):
12345 2345 345 45 5

Same shape, other characters: the Inverted Descending Number Triangle, Inverted Number Triangle, Inverted Repeated Number Triangle, Inverted Reverse Number Triangle and Inverted Right Triangle Star Pattern 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 suffix number triangle works
The last number of every row is n: row i starts at i and has n β i + 1 numbers, so it ends at i + (n β i + 1) β 1 = n. The rows are printed from the left edge, so the column under each number changes from row to row. Add i β 1 spaces in front and each number lines up under itself instead.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n β i + 1numbers (valuei + j β 1). - Move to the next line.
Key takeaways
- Each row is the first row with its first
i β 1numbers removed. - Row
istarts atiand always ends atn. - Adding
i β 1leading spaces lines the numbers up in columns. - It is the number version of the string suffix triangle.
Suffix 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 <= n - i + 1; j++)
printf("%d", i + j - 1);
printf("\n");
}
return 0;
}Suffix 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 <= n - i + 1; j++)
cout << i + j - 1;
cout << endl;
}
return 0;
}Suffix Number Triangle in Java
public class SuffixNumberTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++)
System.out.print(i + j - 1);
System.out.println();
}
}
}Suffix Number Triangle in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n - i + 2):
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
How do I keep each number in its own column?
Print i β 1 spaces before each row. Then every 5 is in the last column, every 4 in the one before it, and the shape becomes a right-aligned inverted triangle.
What is the upright version (5, 45, 345)?
Run the rows the other way: row i prints i numbers starting at n β i + 1. That gives 5, 45, 345, 2345, 12345.
Can I print letters instead?
Yes. Print the letter 'A' + (i + j β 2) instead of the number. The rows become ABCDE, BCDE, CDE, DE, E.
How is it different from the inverted number triangle?
The inverted number triangle (12345, 1234, 123) keeps the start and cuts the end of each row. This pattern keeps the end and cuts the start. Both shrink by one number per row, but here every row finishes with 5 and there the rows always begin with 1.






