- How to print Floyd's triangle using nested for loops
- How to print consecutive natural numbers in a triangular pattern
- How the outer loop controls rows and the inner loop controls columns
- How to maintain a running number across all rows
- How to format the output using spaces
- Why Floyd's triangle is a common nested-loop practice problem
How Floyd's Triangle Works
Floyd's triangle is a right-angled triangular pattern made using consecutive natural numbers. It starts with 1, and every following number is one greater than the previous number.
The first row contains one number, the second row contains two numbers, the third row contains three numbers, and so on. For example, a Floyd's triangle with 5 rows looks like this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
The program uses two loops: an outer loop for rows and an inner loop for columns. A separate variable keeps track of the next number to print, so the numbering continues smoothly from one row to the next.
💡 In one line: Use an outer loop for rows, an inner loop for columns, and print an increasing number after every position.
The Three Steps of Printing the Triangle
| Step | Loop operation | Effect |
|---|---|---|
| 1. Control the rows | for (i = 1; i <= rows; i++) | Creates one row in each iteration |
| 2. Print the numbers | for (j = 1; j <= i; j++) | Prints exactly i numbers in row i |
| 3. Increase the number | number++; | Ensures the next number is always one greater |
The number variable must be declared outside both loops. If it is declared inside the inner loop, it will reset for every column and the triangle will not contain consecutive numbers.
Floyd's triangle is useful for practicing nested loops because the number of values in each row depends on the row number. Row 1 has 1 value, row 2 has 2 values, row 3 has 3 values, and so on.
Complete C Program
Program: print Floyd's triangle using nested for loops
#include <stdio.h>
int main(void) {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}
Program Output
Enter the number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A second run, with 4 rows:
Enter the number of rows: 4
1
2 3
4 5 6
7 8 9 10
The program prints consecutive natural numbers from 1 onward. Row 1 contains one number, row 2 contains two numbers, row 3 contains three numbers, and row 4 contains four numbers.
Step-by-Step Explanation
Let us understand how the program prints Floyd's triangle for 5 rows.
1. Initial value
The variable number is initialized to 1. This is the first number to be printed. The variable rows stores the total number of rows entered by the user.
2. First row
When i = 1, the inner loop runs once. It prints 1 and increases number to 2. Then the program prints a newline.
3. Second row
When i = 2, the inner loop runs twice. It prints 2 and 3. After each print, number is increased. Then the program prints a newline.
4. Third row
When i = 3, the inner loop runs three times. It prints 4, 5, and 6. Then the program prints a newline.
5. Fourth and fifth rows
When i = 4, the program prints 7 8 9 10. When i = 5, it prints 11 12 13 14 15. The numbering continues without restarting.
6. Why the inner loop runs i times
The condition j <= i makes the inner loop run exactly i times. Therefore, row 1 prints one number, row 2 prints two numbers, row 3 prints three numbers, and so on.
7. Why number is declared outside the loops
The variable number must remain unchanged between rows. Since it is declared before the loops, its value continues from the last printed number into the next row.
8. Why a newline is printed after the inner loop
The statement printf("\n"); is placed after the inner loop but inside the outer loop. This moves the output to the next line after each row is completed.
9. Total numbers printed
For n rows, Floyd's triangle contains \(1 + 2 + 3 + \dots + n\) numbers. This total is calculated using the formula \(n(n + 1) / 2\). For 5 rows, the total number of values is 15.
10. Complexity summary
Time complexity is O(n2), because the program prints 1 number in the first row, 2 numbers in the second row, and so on. Space complexity is O(1), because the program uses only a few integer variables and does not store the triangle in an array.
🎯 Remember this: Use an outer loop for rows, an inner loop for columns, and keep a running variable starting from 1. Print it, increase it, and continue until all rows are complete.
Frequently Asked Questions
1. What is Floyd's triangle?
Floyd's triangle is a right-angled triangular pattern of consecutive natural numbers. It starts with 1, and each following number increases by 1.
2. Why are nested loops used?
Nested loops are used because the program must handle both rows and columns. The outer loop controls the row number, while the inner loop prints the required number of values in that row.
3. Why does the inner loop run i times?
Row i contains exactly i numbers. Therefore, the inner loop condition j <= i ensures that the correct number of values is printed in each row.
4. What happens if number is declared inside the inner loop?
If number is declared inside the inner loop, it will be reinitialized every time the inner loop runs. The output will not contain consecutive numbers, so the triangle will be incorrect.
5. How many numbers are printed in an n-row Floyd's triangle?
An n-row Floyd's triangle contains \(n(n + 1) / 2\) numbers. For example, a 5-row triangle contains 15 numbers.
6. What is the time complexity of this program?
The time complexity is O(n2), because the total number of printed values is proportional to \(n(n + 1) / 2\). Space complexity is O(1), since no array is used.
💡 Tip: After implementing this program, try printing a Floyd's triangle using only odd numbers, even numbers, or alphabets. The loop structure remains the same; only the value being printed changes.