In this tutorial, you will learn how to print Floyd's Triangle in Java using nested loops. Floyd's Triangle is a right-angled triangular pattern of natural numbers, named after Robert W. Floyd.
For example, if the number of rows is 4, the Floyd's Triangle will look like this:
You are given a number n and the task of the program is to Print Floyd's Triangle up to n rows.
What is Floyd's Triangle?
Floyd's Triangle is a right-angled triangular arrangement of natural numbers. It starts with 1 in the first row, then 2 3 in the second row, 4 5 6 in the third row, and so on. Each row contains exactly as many numbers as its row number.
Key properties of Floyd's Triangle:
Method 1: Floyd's Triangle in Java Using Nested For Loop
This is the most common and straightforward method. We use two nested for loops — the outer loop controls the rows and the inner loop prints the numbers in each row.
Method 2: Floyd's Triangle in Java Using While Loop
We can also print Floyd's Triangle using a while loop. The logic is the same, only the loop syntax changes.
Method 3: Floyd's Triangle in Java Using a Function
In this method, we move the logic into a separate function to make the code cleaner and reusable.
Comparison of All Methods
Frequently Asked Questions
Q1. What is Floyd's Triangle in Java?
Floyd's Triangle is a right-angled triangular arrangement of natural numbers where the first row has 1 number, the second row has 2 numbers, and so on up to n rows.
Q2. How many numbers are in the nth row of Floyd's Triangle?
The nth row always contains exactly n numbers. The total numbers printed for n rows is n*(n+1)/2.
Q3. What type of loop is used to print Floyd's Triangle?
Floyd's Triangle is printed using nested loops — an outer loop for rows and an inner loop for columns. Both for loop and while loop can be used.
Q4. What is the time complexity of Floyd's Triangle program?
The time complexity is O(n²) because of the two nested loops, where n is the number of rows.
Conclusion
In this tutorial, you learned how to print Floyd's Triangle in Java using three different methods — nested for loop, while loop, and a function. The nested for loop method is the most commonly used and easiest to understand for beginners.
If this post helped you, share it with your friends and follow Codeamy for more Java tutorials!
Related Posts:
• Reverse a String in Java
• Linear Search in Java
• Java Tutorials – Learn Java Step by Step
1 2 3 4 5 6 7 8 9 10
• The first row has 1 number.
• The second row has 2 numbers.
• The nth row has n numbers.
• Numbers are filled sequentially from left to right, top to bottom.
• The second row has 2 numbers.
• The nth row has n numbers.
• Numbers are filled sequentially from left to right, top to bottom.
import java.util.Scanner;
public class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows of Floyd's triangle");
n = in.nextInt();
System.out.println("Floyd's triangle :-");
for ( c = 1 ; c <= n ; c++ )
{
for ( d = 1 ; d <= c ; d++ )
{
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Output
Enter the number of rows of Floyd's triangle 4 Floyd's triangle :- 1 2 3 4 5 6 7 8 9 10Explanation:
•
• The outer loop
• The inner loop
•
num starts at 1 and keeps incrementing for every number printed.• The outer loop
c runs from 1 to n, controlling the row number.• The inner loop
d runs from 1 to c, printing exactly c numbers per row.•
System.out.println() moves to the next line after each row.
import java.util.Scanner;
public class FloydTriangleWhile
{
public static void main(String[] args)
{
int n, num = 1, c = 1, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows of Floyd's triangle");
n = in.nextInt();
System.out.println("Floyd's triangle :-");
while ( c <= n )
{
d = 1;
while ( d <= c )
{
System.out.print(num + " ");
num++;
d++;
}
System.out.println();
c++;
}
}
}
Output
Enter the number of rows of Floyd's triangle 4 Floyd's triangle :- 1 2 3 4 5 6 7 8 9 10Explanation: The outer
while loop handles the rows and the inner while loop prints numbers in each row. The variable num is incremented continuously to fill numbers sequentially.
public class FloydTriangleFunction
{
public static void printFloyd(int n)
{
int num = 1;
for (int c = 1; c <= n; c++)
{
for (int d = 1; d <= c; d++)
{
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
public static void main(String[] args)
{
int rows = 5;
System.out.println("Floyd's triangle for " + rows + " rows:");
printFloyd(rows);
}
}
Output
Floyd's triangle for 5 rows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Explanation: The
printFloyd(int n) method takes the number of rows as a parameter and prints the triangle. This approach keeps the main() method clean and makes the logic reusable.
| Method | Loop Type | Ease of Use | Best For |
|---|---|---|---|
| Nested For Loop | For Loop | Easy | Beginners |
| While Loop | While Loop | Easy | While loop practice |
| Using Function | For Loop | Moderate | Clean & reusable code |
Floyd's Triangle is a right-angled triangular arrangement of natural numbers where the first row has 1 number, the second row has 2 numbers, and so on up to n rows.
The nth row always contains exactly n numbers. The total numbers printed for n rows is n*(n+1)/2.
Floyd's Triangle is printed using nested loops — an outer loop for rows and an inner loop for columns. Both for loop and while loop can be used.
The time complexity is O(n²) because of the two nested loops, where n is the number of rows.
• Reverse a String in Java
• Linear Search in Java
• Java Tutorials – Learn Java Step by Step
Tags:
java