Illustration
To better understand the structure of Floyd's Triangle, let's visualize it with a larger example. Here is what Floyd's Triangle looks like with 8 rows:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
As you can see, the triangle starts with 1 at the top & increases the numbers as you move down the rows. Each row has one more number than the row above it. The numbers in each row are consecutive, starting from the next natural number after the last number in the previous row.
Let’s take a look at a few important observations about Floyd's Triangle:
1. The first row always contains the number 1.
2. Each row starts with the next natural number after the last number in the previous row.
3. The numbers in each row are consecutive & increase by 1.
4. The number of integers in each row is equal to the row number. For example, row 1 has 1 number, row 2 has 2 numbers, & so on.
5. The last number in each row is equal to the sum of the row number & the last number in the previous row.
Algorithm
To generate Floyd's Triangle, we can follow a simple algorithm using nested loops. Let’s discuss this algorithm step by step:
1. Initialize a variable `rows` to store the number of rows in the triangle.
2. Initialize a variable `num` to 1. This variable will keep track of the current number to be printed.
3. Outer loop: Iterate from 1 to `rows` (inclusive). Let's call this loop variable `i`.
- Inner loop: Iterate from 1 to `i` (inclusive). Let's call this loop variable `j`.
- Print the current value of `num` followed by a space.
- Increment `num` by 1.
- Print a new line after each inner loop to move to the next row.
The pseudocode for the algorithm is:
Initialize rows to the desired number of rows
Initialize num to 1
for i = 1 to rows do
for j = 1 to i do
print num followed by a space
increment num by 1
print a new line
By following this algorithm, we can generate Floyd's Triangle with the specified number of rows. The outer loop determines the number of rows, while the inner loop prints the numbers in each row, incrementing the `num` variable after each number is printed.
Implementation
Now, let's see how we can implement Floyd's Triangle in Java using the algorithm we already discussed above.
The Java code to generate Floyd's Triangle is:
public class FloydsTriangle {
public static void main(String[] args) {
int rows = 5; // Number of rows in Floyd's Triangle
int num = 1; // Starting number
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop for columns
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println(); // Move to the next row
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this example :
1. We declare a class named `FloydsTriangle` with the `main` method, which is the program's entry point.
2. Inside the `main` method, we declare an integer variable `rows` to store the number of rows in Floyd's Triangle. In this example, we set it to 5.
3. We initialize an integer variable `num` to 1, which will keep track of the current number to be printed.
4. We start the outer loop that iterates from 1 to `rows` (inclusive). This loop represents the rows of the triangle.
5. Inside the outer loop, we start the inner loop that iterates from 1 to `i` (inclusive), where `i` is the current row number. This loop represents the columns within each row.
6. Inside the inner loop, we print the current value of `num` followed by a space using `System.out.print()`.
7. We increment `num` by 1 after printing each number.
8. After the inner loop ends, we print a new line using `System.out.println()` to move to the next row.
9. The outer loop continues until all the rows are printed.
When we run this code, it will generate Floyd's Triangle with 5 rows:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
You can easily modify the number of rows by changing the value of the `rows` variable in the code.
Time and Space Complexity
Time Complexity: The time complexity for generating Floyd's Triangle is O(n^2), where 'n' is the number of rows. This is because, for each row, the number of operations (printing numbers) is proportional to the row number itself. Hence, for the first row, there is 1 operation; for the second row, there are 2 operations; for the third row, 3 operations, and so forth, leading to a series sum that approximates n(n + 1)/2, which simplifies to O(n^2).
Space Complexity: The space complexity for generating Floyd’s Triangle is O(1), assuming the output does not need to be stored. If only printing the triangle to the console, no additional space proportional to the input size is required beyond the few temporary variables needed for iteration and number generation.
Frequently Asked Questions
Can we generate Floyd's Triangle with a different starting number?
Yes, you can modify the code to start with any desired number by initializing the `num` variable with that number instead of 1.
How can we print Floyd's Triangle in reverse order?
To print Floyd's Triangle in reverse order, you can modify the outer loop to iterate from `rows` to 1 (inclusive) and adjust the inner loop accordingly.
Can we generate Floyd's Triangle with a different increment pattern?
Yes, you can modify the code to increment `num` by any desired value after each number is printed, resulting in a different increment pattern in the triangle.
Conclusion
In this article, we discussed Floyd's Triangle, a classic pattern in computer programming. We learned what Floyd's Triangle is, understood its structure with the help of an illustration, and its algorithm to generate it. We implemented the algorithm in Java using nested loops and analyzed the time and space complexity of the code. Floyd's Triangle is a good programming exercise for practicing nested loops and pattern printing.
You can also check out our other blogs on Code360.