Floyds Triangle

Java coding interview problem for Pattern Printing: Floyds Triangle.

Floyd's Triangle is one of the most popular number pattern programs asked in Java coding interviews.

Unlike star patterns, Floyd's Triangle prints continuous natural numbers in a triangular format.

This pattern is an excellent exercise for understanding:

  • Nested loops
  • Number progression
  • Row and column relationships
  • Variable updates
  • Logical thinking

Interviewers often ask Floyd's Triangle because it tests your ability to manage loops and continuously update values.

Learning Floyd's Triangle also helps in understanding:

  • Number Pattern Problems
  • Matrix Traversal
  • Dynamic Programming Basics
  • Nested Loop Logic
  • Pattern Recognition

What is Floyd's Triangle?

Floyd's Triangle is a right-angled triangular arrangement of consecutive natural numbers.

Each row contains one more number than the previous row.

Example

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Notice:

  • Numbers are continuous.
  • No number repeats.
  • Every row contains one additional number.

History of Floyd's Triangle

Floyd's Triangle is named after the American computer scientist Robert W. Floyd.

Robert Floyd was well known for his contributions to:

  • Graph Algorithms
  • Compiler Design
  • Dynamic Programming
  • Algorithm Optimization

Although Floyd did not invent this pattern for interviews, it became popular because it clearly demonstrates nested-loop concepts.


Why is Floyd's Triangle Asked in Interviews?

Interviewers use Floyd's Triangle to evaluate whether candidates can:

  • Work with nested loops
  • Maintain a running counter
  • Understand row-column relationships
  • Print dynamic output
  • Solve sequential number problems

It is considered one of the best beginner-friendly pattern problems.


Understanding Rows and Number Progression

Suppose

Rows = 5

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Observe the pattern.

Row Numbers Printed
1 1
2 2 3
3 4 5 6
4 7 8 9 10
5 11 12 13 14 15

Notice:

  • Row 1 contains 1 number.
  • Row 2 contains 2 numbers.
  • Row 3 contains 3 numbers.
  • Row 4 contains 4 numbers.
  • Row 5 contains 5 numbers.

Mathematical Pattern

If the current row is

r

then the number of elements printed is

r

The value printed is simply a continuously increasing counter.

Example

counter = 1

↓

2

↓

3

↓

4

↓

5

↓

6

↓

7

The counter is incremented after printing every number.


Visual Representation

For

Rows = 5
Row 1

Print

1

--------------------

Row 2

Print

2 3

--------------------

Row 3

Print

4 5 6

--------------------

Row 4

Print

7 8 9 10

--------------------

Row 5

Print

11 12 13 14 15

Observe that:

  • The number of columns equals the row number.
  • The counter never resets.

Pattern Output

Input

Rows = 5

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Understanding the Logic

There are only two things happening.

Step 1

Print the required number of values.

The number of values equals the current row.

Step 2

Increase the counter after every print.

Example

Initially

counter = 1

Row 1

Print

1

counter = 2

Row 2

Print

2

Print

3

counter = 4

Row 3

Print

4

Print

5

Print

6

counter = 7

The same process repeats.


Brute Force Approach

The simplest solution uses two nested loops.

Outer loop

Print rows.

Inner loop

Print numbers.

Maintain one counter variable outside the loops.

Increment the counter after every print.


Algorithm

Step 1

Read the number of rows.

rows = 5;

Step 2

Initialize

counter = 1;

Step 3

Start the outer loop.

1

↓

Rows

Each iteration represents one row.


Step 4

Start the inner loop.

1

↓

Current Row

Each iteration prints one number.


Step 5

Print

counter

Increment

counter++

Step 6

Move to the next line.

Repeat until all rows are completed.


Dry Run

Input

Rows = 4

Initially

counter = 1

Row 1

Print

1

counter = 2

Row 2

Print

2

Print

3

counter = 4

Row 3

Print

4

Print

5

Print

6

counter = 7

Row 4

Print

7

Print

8

Print

9

Print

10

Final Output

1
2 3
4 5 6
7 8 9 10

Approach 1 — Using Nested Loops

This is the standard interview solution.


Complete Java Program

public class FloydsTriangle {

    public static void main(String[] args) {

        int rows = 5;

        int counter = 1;

        for (int i = 1; i <= rows; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(counter + " ");

                counter++;

            }

            System.out.println();

        }

    }

}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Step-by-Step Code Explanation

Step 1

Declare the number of rows.

int rows = 5;

Step 2

Initialize the counter.

int counter = 1;

This variable stores the next number to print.


Step 3

Create the outer loop.

for (int i = 1; i <= rows; i++)

The outer loop controls the number of rows.

Iterations

1

2

3

4

5

Step 4

Create the inner loop.

for (int j = 1; j <= i; j++)

The inner loop prints numbers equal to the current row.

Example

Row Numbers Printed
1 1
2 2
3 3
4 4
5 5

Step 5

Print the counter.

System.out.print(counter + " ");

Then increment it.

counter++;

This ensures every printed number is unique and sequential.


Example Execution

Input

Rows = 3

Output

1
2 3
4 5 6

Input

Rows = 6

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

Why Does This Work?

The algorithm uses nested loops to control both the number of rows and the number of values printed in each row.

  • The outer loop determines the current row.
  • The inner loop prints exactly as many numbers as the current row number.
  • A single counter variable is shared across all rows. Instead of resetting for every row, it keeps increasing after each printed value.

This continuous increment creates the characteristic sequential number pattern of Floyd's Triangle.


Advantages of This Approach

  • Very easy to understand.
  • Uses simple nested loops.
  • Excellent for learning loop control.
  • Frequently asked in Java interviews.
  • Easy to extend into Reverse Floyd's Triangle and other number patterns.
  • Uses only O(1) extra memory.

Drawbacks

Although this is the standard interview solution, interviewers often ask additional questions such as:

  • Can you create a reusable printFloydsTriangle() method?
  • Can you print Reverse Floyd's Triangle?
  • Can you print a Right-Aligned Floyd's Triangle?
  • Can you generate the triangle without using a separate counter variable?
  • What is the time complexity?
  • Can you store the output in a 2D array?

In Part 2, we'll cover:

  • Optimized Approach
  • Reusable printFloydsTriangle() Method
  • Reverse Floyd's Triangle
  • Right-Aligned Floyd's Triangle
  • Continuous Floyd's Triangle
  • Time and Space Complexity
  • Comparison of Approaches
  • Common Interview Mistakes
  • Interview Follow-up Questions
  • Related Pattern Problems
  • Key Takeaways
  • Interview Tips

Approach 2 — Optimized Approach

The standard solution uses a separate counter variable that increases after every printed number.

This approach is already optimal because:

  • Each number is printed exactly once.
  • No unnecessary calculations are performed.
  • Only one additional variable is used.

The time complexity cannot be improved because every number must be printed.


Java Program

public class FloydsTriangleOptimized {

    public static void main(String[] args) {

        int rows = 5;

        int number = 1;

        for (int i = 1; i <= rows; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(number + " ");

                number++;

            }

            System.out.println();

        }

    }

}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Approach 3 — Using a Reusable Method

Instead of writing the logic inside the main() method, create a reusable method.

This improves:

  • Code readability
  • Reusability
  • Maintainability
  • Unit testing

Java Program

public class FloydsTriangleMethod {

    static void printFloydsTriangle(int rows) {

        int number = 1;

        for (int i = 1; i <= rows; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(number + " ");

                number++;

            }

            System.out.println();

        }

    }

    public static void main(String[] args) {

        printFloydsTriangle(5);

    }

}

Pattern Variation 1 — Reverse Floyd's Triangle

Instead of printing numbers in increasing rows, print them in decreasing rows.


Output

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Java Program

public class ReverseFloydsTriangle {

    public static void main(String[] args) {

        int rows = 5;

        int number = 1;

        for (int i = rows; i >= 1; i--) {

            for (int j = 1; j <= i; j++) {

                System.out.print(number + " ");

                number++;

            }

            System.out.println();

        }

    }

}

Pattern Variation 2 — Right-Aligned Floyd's Triangle

Print the triangle aligned to the right.


Output

        1
      2 3
    4 5 6
  7 8 9 10
11 12 13 14 15

Java Program

public class RightAlignedFloydsTriangle {

    public static void main(String[] args) {

        int rows = 5;

        int number = 1;

        for (int i = 1; i <= rows; i++) {

            // Print leading spaces
            for (int j = rows; j > i; j--) {

                System.out.print("  ");

            }

            // Print numbers
            for (int j = 1; j <= i; j++) {

                System.out.print(number + " ");

                number++;

            }

            System.out.println();

        }

    }

}

Pattern Variation 3 — Continuous Floyd's Triangle

Print the numbers continuously without resetting or formatting changes.


Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

The standard Floyd's Triangle is itself a continuous number pattern.


Dry Run

Input

Rows = 3

Initially

number = 1

Processing

Row 1

Print

1

number = 2

----------------

Row 2

Print

2

Print

3

number = 4

----------------

Row 3

Print

4

Print

5

Print

6

number = 7

Final Output

1
2 3
4 5 6

Time Complexity

Suppose

n

is the number of rows.


Standard Floyd's Triangle

Operation Complexity
Time O(n²)
Space O(1)

Reverse Floyd's Triangle

Operation Complexity
Time O(n²)
Space O(1)

Right-Aligned Floyd's Triangle

Operation Complexity
Time O(n²)
Space O(1)

Comparison of Approaches

Approach Time Space Recommended
Basic Nested Loops O(n²) O(1) ✅ Best for Beginners
Reusable Method O(n²) O(1) Production Ready
Reverse Floyd's Triangle O(n²) O(1) Intermediate
Right-Aligned Floyd's Triangle O(n²) O(1) Interview Favorite

Common Mistakes

Mistake 1

Resetting the counter inside the outer loop.

Wrong

for (int i = 1; i <= rows; i++) {

    int number = 1;

}

Correct

int number = 1;

Declare it before the outer loop.


Mistake 2

Using the wrong inner loop condition.

Wrong

j < i

Correct

j <= i

Mistake 3

Forgetting to increment the counter.

Wrong

System.out.print(number);

Correct

System.out.print(number);

number++;

Mistake 4

Incrementing before printing.

Wrong

number++;

System.out.print(number);

Correct

System.out.print(number);

number++;

Mistake 5

Printing all numbers on one line.

Always use

System.out.println();

after completing each row.


Interview Follow-up Questions

Q1. What is Floyd's Triangle?

Q2. Why is a counter variable required?

Q3. Can you generate the pattern without using an array?

Q4. Can you print Reverse Floyd's Triangle?

Q5. Can you print a Right-Aligned Floyd's Triangle?

Q6. What is the time complexity?

Q7. Can you store the pattern in a 2D array?

Q8. Can you print only odd numbers?

Q9. Can you print only even numbers?

Q10. Can you replace numbers with alphabets?


Related Pattern Problems

  • Pascal's Triangle
  • Full Pyramid Pattern
  • Inverted Pyramid Pattern
  • Diamond Pattern
  • Number Pyramid
  • Alphabet Pyramid
  • Right Triangle Pattern
  • Reverse Triangle Pattern
  • Zig-Zag Number Pattern

Key Takeaways

  • Floyd's Triangle prints continuous natural numbers.
  • The row number determines how many values are printed.
  • A single counter variable is shared across all rows.
  • The counter is incremented after every printed value.
  • The algorithm uses nested loops and runs in O(n²) time with O(1) extra space.
  • Floyd's Triangle is one of the best exercises for mastering nested loops and sequential number generation.

Frequently Asked Interview Questions

Q1. Why do we need a separate counter variable?

The counter keeps track of the next number to print. Since it is shared across all rows and never resets, the sequence remains continuous.


Q2. Why doesn't the counter reset for each row?

Resetting the counter would restart the sequence at 1 for every row, which would no longer produce Floyd's Triangle.


Q3. Can Floyd's Triangle be generated without nested loops?

No.

Each row contains a different number of elements, so nested loops are the most natural and efficient solution.


Q4. Can we print odd or even numbers instead?

Yes.

Instead of incrementing the counter by 1, increment it by 2.

Example:

number += 2;

Initialize:

int number = 1;

for odd numbers, or

int number = 2;

for even numbers.


Q5. How is Floyd's Triangle different from Pascal's Triangle?

Floyd's Triangle Pascal's Triangle
Prints consecutive natural numbers. Prints Binomial Coefficients.
Uses a simple incrementing counter. Uses mathematical relationships between adjacent values.
Very simple nested-loop logic. Requires combinatorial or dynamic programming logic.
Focuses on sequential numbering. Focuses on mathematical computation.

Interview Tip

If an interviewer asks:

"Print Floyd's Triangle in Java."

Start by explaining that the solution requires two nested loops:

  1. The outer loop controls the number of rows.
  2. The inner loop prints exactly as many numbers as the current row.

Then explain that a single counter variable is initialized before the loops and incremented after every printed value. This creates a continuous sequence of natural numbers without resetting.

Finally, mention that the same logic can be extended to generate Reverse Floyd's Triangle, Right-Aligned Floyd's Triangle, Odd/Even Floyd's Triangle, and other number-based patterns with only small modifications to the loop structure.