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
Triangular Number Triangle output
Output (n = 5):
1 1 3 1 3 6 1 3 6 10 1 3 6 10 15

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.
- Run the outer loop for
ifrom 1 ton. - Set
sum = 0. - For
jfrom 1 toi: addjtosumand print it three characters wide. - Move to the next line.
Key takeaways
- Each value is the sum 1 + 2 + … +
j, also given byj(j + 1) / 2. - Resetting
suminside 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
iis also the last number in rowiof 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.










