Number Triangle Pattern: flowchart, algorithm and program

This program reads a number n and prints a triangle where row i holds the numbers 1 to i. It uses two nested loops: the outer loop picks the row, and the inner loop prints the numbers in that row.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: Set i = 1.
  4. Step 4: If i <= n, go to step 5; otherwise go to step 11.
  5. Step 5: Set j = 1.
  6. Step 6: If j <= i, go to step 7; otherwise go to step 9.
  7. Step 7: Print j.
  8. Step 8: Set j = j + 1. Go to step 6.
  9. Step 9: Print newline.
  10. Step 10: Set i = i + 1. Go to step 4.
  11. Step 11: Stop.

Pseudocode

BEGIN
    READ n
    i ← 1
    WHILE i <= n DO
        j ← 1
        WHILE j <= i DO
            PRINT j
            j ← j + 1
        END WHILE
        PRINT newline
        i ← i + 1
    END WHILE
END

Program in C, Python and Java

Number Triangle Pattern program in C

#include <stdio.h>

int main() {
    int n, i, j;
    printf("Enter n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}

Number Triangle Pattern program in Python

n = int(input("Enter n: "))
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

Number Triangle Pattern program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Sample input: n = 5
Output: Enter n: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

How it works

  • The outer loop runs i from 1 to n. Each value of i is one row of the triangle.
  • For every row, the inner loop runs j from 1 to i, so row 1 has one number, row 2 has two, and so on.
  • Inside the inner loop, printf("%d ", j) prints the current j followed by a space, so the numbers in a row stay on one line.
  • After the inner loop finishes, printf("\n") moves the cursor to the next line, so the next row starts fresh.
  • For n = 5 the inner loop runs 1 + 2 + 3 + 4 + 5 = 15 times in total, and the program prints 5 rows.

Dry run (trace table)

StepLineBoxnijOutput
13Start
26Read n5Enter n:
37i = 151
47i <= n?51
58j = 1511
68j <= i?511
79Print j5111
88j = j + 1512
98j <= i?512
1011Print newline512
117i = i + 1522
127i <= n?522
138j = 1521
148j <= i?521
159Print j5211
168j = j + 1522
178j <= i?522
189Print j5222
198j = j + 1523
208j <= i?523

Time and space complexity

Time: O(n²) · Space: O(1). The inner loop runs i times for each i from 1 to n, giving n(n+1)/2 prints, and only a few variables are used.

Common mistakes

  • Printing i instead of j in the inner loop. That gives 1, 2 2, 3 3 3 rather than 1, 1 2, 1 2 3.
  • Starting j from 0 or using j < i. This prints an extra number or one too few in each row.
  • Putting the newline inside the inner loop, or leaving it out. Then every number lands on its own line, or the whole triangle prints on a single line.

Viva questions

Q1. What does this program print for n = 4?

It prints four rows: '1', '1 2', '1 2 3' and '1 2 3 4', each on its own line. Row i has the numbers 1 up to i.

Q2. What is the job of the outer loop and what is the job of the inner loop?

The outer loop (variable i) picks the row number from 1 to n. The inner loop (variable j) prints the numbers 1 to i within that row.

Q3. Why is the inner loop condition j <= i and not j <= n?

Row i must contain only i numbers, so the inner loop is limited by the current row. With j <= n, every row would print n numbers and you would get a square, not a triangle.

Q4. Why is printf("\n") placed after the inner loop and not inside it?

The newline should end a row only once, after all of its numbers are printed. Inside the inner loop, every number would land on its own line.

Q5. What happens if the user enters n = 0 or a negative number?

The outer loop condition i <= n is false at the start, so nothing is printed and the program ends. There is no error message, because the program doesn't validate the input.

Q6. Why do both loops start at 1 and not at 0?

Row numbers and the printed values both start at 1. Starting i at 0 would print an empty first row, and starting j at 0 would print a 0 in each row.

Q7. What is the time complexity of this program, and why?

It is O(n²). The inner loop runs 1 + 2 + ... + n = n(n+1)/2 times in total, so printf is called about n²/2 times.

Q8. What would you change to print an inverted triangle, with n numbers in the first row and 1 in the last?

Either run the outer loop from n down to 1 (for i = n; i >= 1; i--) and keep j <= i, or keep the outer loop and make the inner loop go from 1 to n - i + 1. To print the row number i repeated, you would print i instead of j.

Frequently asked questions

Why do we need two loops in a number triangle?

One loop is needed for the rows and another for the numbers inside each row. The inner loop's limit depends on the row number i, which is what makes the shape a triangle.

How do I print the same number in each row instead (1, 2 2, 3 3 3)?

Print i instead of j inside the inner loop: printf("%d ", i). The inner loop still runs i times, so row i shows the value i repeated i times.

What is the time complexity of this program?

The inner loop runs 1 + 2 + ... + n = n(n+1)/2 times, so the time complexity is O(n^2).

More programs

← All programs

Try AI Flowchart free →