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

Suffix Number Triangle (12345, 2345, 345)

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
  1. Suffix Number Triangle output
  2. How the suffix number triangle works
  3. Suffix Number Triangle in C
  4. Suffix Number Triangle in C++
  5. Suffix Number Triangle in Java
  6. Suffix Number Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Suffix Number Triangle output

Output (n = 5):

12345
2345
345
45
5
Suffix Number Triangle layout diagram, row by row (n = 5)
How each row of the suffix number triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

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

Key takeaways

  • Each row is the first row with its first i βˆ’ 1 numbers removed.
  • Row i starts at i and always ends at n.
  • Adding i βˆ’ 1 leading 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.

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