Floyd’s Triangle: flowchart, algorithm and program
This program reads n and prints Floyd's triangle, where numbers start at 1 and keep increasing across rows, and row i holds exactly i numbers. The idea is to use one counter, num, that never resets, plus two loops: the outer loop picks the row and the inner loop prints that many numbers.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set num = 1.
- Step 3: Read n.
- Step 4: Set i = 1.
- Step 5: If i <= n, go to step 6; otherwise go to step 13.
- Step 6: Set j = 1.
- Step 7: If j <= i, go to step 8; otherwise go to step 11.
- Step 8: Print num.
- Step 9: Set num = num + 1.
- Step 10: Set j = j + 1. Go to step 7.
- Step 11: Print newline.
- Step 12: Set i = i + 1. Go to step 5.
- Step 13: Stop.
Pseudocode
BEGIN
num ← 1
READ n
i ← 1
WHILE i <= n DO
j ← 1
WHILE j <= i DO
PRINT num
num ← num + 1
j ← j + 1
END WHILE
PRINT newline
i ← i + 1
END WHILE
END
Program in C, Python and Java
Floyd’s Triangle program in C
#include <stdio.h>
int main() {
int n, i, j, num = 1;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}Floyd’s Triangle program in Python
n = int(input("Enter n: "))
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()Floyd’s Triangle 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();
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Sample input: n = 4
Output: 1
2 3
4 5 6
7 8 9 10
How it works
- num starts at 1 and is the only value that gets printed. It is never reset, so the numbers continue from one row to the next.
- The outer loop runs i from 1 to n. Each pass of this loop is one row of the triangle.
- The inner loop runs j from 1 to i, so row i prints exactly i numbers. Each time, it prints num and then does num++.
- After the inner loop finishes, printf("\n") moves to the next line so the next row starts fresh.
- For n = 4 the rows are 1 / 2 3 / 4 5 6 / 7 8 9 10, and num ends at 11. The total count printed is n(n+1)/2 = 10.
Dry run (trace table)
| Step | Line | Box | num | n | i | j | Output |
|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||
| 2 | 4 | num = 1 | 1 | ||||
| 3 | 6 | Read n | 1 | 4 | Enter n: | ||
| 4 | 7 | i = 1 | 1 | 4 | 1 | ||
| 5 | 7 | i <= n? | 1 | 4 | 1 | ||
| 6 | 8 | j = 1 | 1 | 4 | 1 | 1 | |
| 7 | 8 | j <= i? | 1 | 4 | 1 | 1 | |
| 8 | 9 | Print num | 1 | 4 | 1 | 1 | 1 |
| 9 | 10 | num = num + 1 | 2 | 4 | 1 | 1 | |
| 10 | 8 | j = j + 1 | 2 | 4 | 1 | 2 | |
| 11 | 8 | j <= i? | 2 | 4 | 1 | 2 | |
| 12 | 12 | Print newline | 2 | 4 | 1 | 2 | |
| 13 | 7 | i = i + 1 | 2 | 4 | 2 | 2 | |
| 14 | 7 | i <= n? | 2 | 4 | 2 | 2 | |
| 15 | 8 | j = 1 | 2 | 4 | 2 | 1 | |
| 16 | 8 | j <= i? | 2 | 4 | 2 | 1 | |
| 17 | 9 | Print num | 2 | 4 | 2 | 1 | 2 |
| 18 | 10 | num = num + 1 | 3 | 4 | 2 | 1 | |
| 19 | 8 | j = j + 1 | 3 | 4 | 2 | 2 | |
| 20 | 8 | j <= i? | 3 | 4 | 2 | 2 |
Time and space complexity
Time: O(n²) · Space: O(1). The inner loop runs 1+2+...+n = n(n+1)/2 times with constant work each time, and only a few variables are used.
Common mistakes
- Resetting num = 1 inside the outer loop, which makes every row start from 1 instead of continuing the sequence.
- Running the inner loop up to n instead of up to i, which prints a full rectangle rather than a triangle.
- Forgetting the newline after the inner loop, so all numbers print on a single line.
Viva questions
Q1. What is Floyd's triangle, and what does your program print for n = 4?
It is a right-angled triangle of consecutive natural numbers, where row i has i numbers. For n = 4 it prints 1 / 2 3 / 4 5 6 / 7 8 9 10, each row on its own line.
Q2. Why is num initialised to 1 and why is it declared outside both loops?
Floyd's triangle starts at 1, so num begins at 1. It sits outside the loops so it is not reset and keeps counting across rows, giving one continuous sequence.
Q3. What is the role of the outer loop and the inner loop?
The outer loop (i) controls the row number, running n times. The inner loop (j) runs i times in each row and prints that row's numbers.
Q4. Why does the inner loop condition use j <= i and not j <= n?
Row i must contain exactly i numbers, so the inner loop is limited by the row number. Using n would print n numbers in every row and give a rectangle, not a triangle.
Q5. What is the purpose of printf("\n") on line 12, and why is it placed outside the inner loop?
It moves the cursor to a new line after each row is complete. Inside the inner loop it would put every number on its own line.
Q6. What happens if the user enters n = 0 or a negative number?
The condition i <= n is false at the start, so the outer loop never runs and nothing is printed. The program doesn't crash, but it doesn't validate the input either. For n = 1 it prints just '1'.
Q7. How many numbers are printed in total, and what is the time complexity?
It prints 1 + 2 + ... + n = n(n+1)/2 numbers, and the last number printed equals that value. The time complexity is O(n²) because the work is proportional to the number of elements printed.
Q8. What would you change to print the triangle in reverse (n numbers in the first row down to 1 in the last), or to right-align the triangle?
For the reverse shape, start the inner loop's limit at n and shrink it, e.g. for (j = 1; j <= n - i + 1; j++). To right-align, print (n - i) leading spaces (or use a width like %3d) before each row. The num counter logic stays the same.
Frequently asked questions
What is Floyd's triangle?
It is a right-angled triangle of consecutive natural numbers starting at 1, where the first row has 1 number, the second has 2, and so on.
Why is num declared outside both loops?
num must keep its value across rows so the numbers continue in order. If it were reset in a loop, the sequence would restart.
How many numbers are printed for a given n, and what is the last one?
A total of n(n+1)/2 numbers are printed, and the last number printed is also n(n+1)/2. For n = 4 that is 10.