Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 61

Floyd's Triangle Program in Java

In this lesson: Print Floyd's triangle in Java with nested loops and a counter — code, dry run and output. In Hindi & English.

What is Floyd's Triangle

Floyd's Triangle is a right-angled triangle of numbers, filled row by row with numbers counting up continuously. Row 1 has one number, row 2 has two, and so on. It is a classic pattern exercise combining nested loops with a running counter.

💡 Row i has i numbers, and a single counter keeps increasing across all rows.

The pattern

For 4 rows, Floyd's Triangle looks like:

1
2 3
4 5 6
7 8 9 10

Notice the numbers never restart — they flow continuously. Row 3 ends at 6, so row 4 begins at 7. Each row has one more number than the last.

The Java program

public class Floyd {
    public static void main(String[] args) {
        int rows = 4;
        int num = 1;                 // running counter

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(num + " ");
                num++;               // never resets
            }
            System.out.println();    // new line per row
        }
    }
}
1
2 3
4 5 6
7 8 9 10

The outer loop i is the row; the inner loop runs i times to print that row's numbers. The key is num, which keeps counting up and is never reset.

Dry run

Dry Run — Floyd's Triangle (num keeps rising), rows=4
Row (i)Printed in that rownum reached
112
22 34
34 5 67
47 8 9 1011
💡 Understand it this way

Row i prints i numbers. The counter num is never reset between rows, so it flows on: row 3 ends at 6, and row 4 continues from 7.

The running counter

The heart of Floyd's Triangle is that num is declared before the loops and increased after every print, but never reset inside a row. This continuous counting is what makes the numbers flow smoothly from one row to the next, unlike patterns that restart each row.

Common mistakes

  • Resetting the counter each row. Declare num outside the loops so it keeps rising.
  • Wrong inner loop limit. Row i needs j <= i to print i numbers.
  • Forgetting the newline. Add println() after each row.
  • Confusing rows and counter. Rows control how many numbers; the counter controls their value.

Summary

  • Floyd's Triangle fills numbers row by row, continuously.
  • Row i has i numbers.
  • Use nested loops: outer for rows, inner for numbers.
  • Keep a single counter that never resets between rows.
  • Add a newline after each row.

Next, try more shapes in nested loops, or a maths pattern in the Armstrong number program.

Frequently Asked Questions

What is Floyd's Triangle?
Floyd's Triangle is a right-angled triangle of natural numbers, filled row by row. Row 1 has 1 number, row 2 has 2, and so on, with the numbers counting up continuously: 1, then 2 3, then 4 5 6, and so on.
How many numbers are in each row?
Row i has exactly i numbers. So row 1 has 1, row 2 has 2, row 3 has 3. The triangle grows wider by one number each row.
How does the counter work in Floyd's Triangle?
A single counter starts at 1 and keeps increasing as you print. It does not reset each row — it continues from where the previous row ended, which gives the continuous numbering.
Why does Floyd's Triangle need nested loops?
The outer loop handles the rows, and the inner loop prints the numbers in each row. The inner loop runs i times for row i, which creates the triangular shape.
What is the difference from a star pattern?
A star pattern prints the same symbol, while Floyd's Triangle prints increasing numbers using a running counter. The loop structure is the same; only what you print differs.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Course complete! Continue withAndroid (Kotlin) Your progress: 61 / 61 (100%) · Recommended next course Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free