0
0
JavaProgramBeginner · 2 min read

Java Program to Print Floyd Triangle with Output

You can print Floyd's triangle in Java using nested for loops and a counter variable, for example: int num = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(num++ + " "); } System.out.println(); }.
📋

Examples

Inputrows = 1
Output1
Inputrows = 3
Output1 2 3 4 5 6
Inputrows = 5
Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
🧠

How to Think About It

To print Floyd's triangle, think of printing numbers in rows where each row has one more number than the previous. Start counting from 1 and increase the count after printing each number. Use two loops: the outer loop for rows and the inner loop for numbers in each row.
📐

Algorithm

1
Get the number of rows to print.
2
Initialize a counter starting at 1.
3
For each row from 1 to the number of rows:
4
Print numbers equal to the current row number, increasing the counter after each print.
5
Move to the next line after each row.
💻

Code

java
public class FloydTriangle {
    public static void main(String[] args) {
        int rows = 5;
        int num = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(num++ + " ");
            }
            System.out.println();
        }
    }
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
🔍

Dry Run

Let's trace printing Floyd's triangle for 3 rows through the code.

1

Initialize variables

rows = 3, num = 1

2

First row (i=1)

Print 1, num becomes 2

3

Second row (i=2)

Print 2 and 3, num becomes 4

4

Third row (i=3)

Print 4, 5, 6, num becomes 7

Row (i)Numbers PrintedValue of num after row
112
22 34
34 5 67
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to the number of rows, deciding how many rows to print.

Step 2: Inner loop prints numbers per row

The inner for loop runs as many times as the current row number, printing numbers in sequence.

Step 3: Counter increments after each print

The variable num increases by 1 after printing each number to keep the sequence continuous.

🔄

Alternative Approaches

Using while loops
java
public class FloydTriangle {
    public static void main(String[] args) {
        int rows = 5;
        int num = 1;
        int i = 1;
        while (i <= rows) {
            int j = 1;
            while (j <= i) {
                System.out.print(num++ + " ");
                j++;
            }
            System.out.println();
            i++;
        }
    }
}
This uses while loops instead of for loops, which some find easier to understand but requires manual incrementing.
Using recursion
java
public class FloydTriangle {
    static int num = 1;
    public static void printRow(int row, int maxRow) {
        if (row > maxRow) return;
        printNumbers(row, 1);
        System.out.println();
        printRow(row + 1, maxRow);
    }
    public static void printNumbers(int count, int current) {
        if (current > count) return;
        System.out.print(num++ + " ");
        printNumbers(count, current + 1);
    }
    public static void main(String[] args) {
        printRow(1, 5);
    }
}
This recursive approach is elegant but more complex and less efficient for beginners.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times cumulatively, resulting in O(n^2) time.

Space Complexity

Only a few variables are used for counting and looping, so space complexity is O(1), constant space.

Which Approach is Fastest?

The for-loop approach is straightforward and efficient; recursion adds overhead and is less practical here.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simple and efficient printing
While loopsO(n^2)O(1)Alternative loop style
RecursionO(n^2)O(n)Elegant but less efficient
💡
Use nested loops with a counter variable to print Floyd's triangle easily.
⚠️
Forgetting to increment the counter after printing each number causes repeated numbers.