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

Triangular Number Triangle (1, 1 3, 1 3 6)

Read summary with AI tools

The triangular number triangle prints the running totals 1, 3, 6, 10 and 15. Row i shows the first i of them, so each row is the one above it plus one new number at the end.

Quick answer: Use a running sum. At the start of each row set sum = 0; then for j from 1 to i, add j to the sum and print it. The numbers you get are the triangular numbers j(j + 1) / 2: 1, 3, 6, 10, 15.

This program is part of the Series Patterns collection.

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

Triangular Number Triangle output

Output (n = 5):

  1
  1  3
  1  3  6
  1  3  6 10
  1  3  6 10 15
Triangular Number Triangle layout diagram, row by row (n = 5)
The numbers of the triangular number triangle laid out row by row (n = 5).

How the triangular number triangle works

A triangular number counts the dots in a triangle with j dots on its bottom side: 1, then 1 + 2 = 3, then 1 + 2 + 3 = 6. The inner loop builds exactly that sum, one step per column, and prints it after every step. Resetting the sum on each row makes every row start again from 1, which is why the triangle reads the same down each column.

  1. Run the outer loop for i from 1 to n.
  2. Set sum = 0.
  3. For j from 1 to i: add j to sum and print it three characters wide.
  4. Move to the next line.

Key takeaways

  • Each value is the sum 1 + 2 + … + j, also given by j(j + 1) / 2.
  • Resetting sum inside the outer loop makes every row restart at 1.
  • Every column holds a single value, so the triangle reads 1, 3, 6, 10, 15 down the diagonal.
  • The last number of row i is also the last number in row i of Floyd’s triangle.

Triangular Number Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Triangular Number Triangle in C++

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n = 5;

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

Triangular Number Triangle in Java

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

        for (int i = 1; i <= n; i++) {
            int sum = 0;
            for (int j = 1; j <= i; j++) {
                sum += j;
                System.out.printf("%3d", sum);
            }
            System.out.println();
        }
    }
}

Triangular Number Triangle in Python

n = 5

for i in range(1, n + 1):
    total = 0
    for j in range(1, i + 1):
        total += j
        print(f"{total:3d}", end="")
    print()

Time and space complexity

The inner loop does one addition per number, so the program runs in O(n²) time with O(1) extra memory.

Frequently asked questions

Why are they called triangular numbers?

Because that many dots can be arranged as a triangle: 1 dot, then 3 (a row of 1 above a row of 2), then 6, then 10, like the pins in ten-pin bowling. The j-th one is j(j + 1) / 2.

Can I print the numbers without the inner sum?

Yes. Print j * (j + 1) / 2 directly for each j. Both versions give the same output; the running sum shows how the numbers are built, the formula is shorter.

How do I continue the totals across rows instead of restarting?

Move sum = 0 and a separate counter outside the outer loop, and add the counter instead of j. The rows then read 1, 3 6, 10 15 21, and so on: where you reset a variable decides the whole pattern.

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.

Latest articles

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.