import java.util.Scanner;
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 you want");
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();
}
}
}
Code Breakdown:
1. Importing Scanner Class:
import java.util.Scanner;
Scanner class is declared to scan for the input from the user.
2. Class Definition:
class FloydTriangle
Here, a class FloydTriangle is declared. The class contains the main method where the execution of the program starts.
3. Main Method:
public static void main(String args[])
The main method is the entry point of the program. It is where the program execution begins.
4. Variable Declarations:
int n, num = 1, c, d;
- n: This will hold the number of rows in Floyd’s triangle the user wishes to see.
- num: This is the variable which will contain the number to be printed for the current row in the triangle. It is initialized with 1.
- c and d: These are loop variables containing the row and column number of the Floyd’s triangle.
5. Scanner Object Creation:
Scanner in = new Scanner(System.in);
This creates a Scanner object named in to read input from the user.
6. Prompting the User for Input:
System.out.println(“Enter the number of rows of floyd’s triangle you want”);
n = in.nextInt();
The program prompts the user to input the number of rows they wish to have in Floyd’s triangle. The input is stored in the variable n.
7. Printing the Header:
System.out.println(“Floyd’s triangle :-“);
This line prints the heading Floyd’s triangle :- to introduce the output.
8. Outer Loop (for rows of the triangle):
for (c = 1; c <= n; c++)
- This for loop controls the number of rows in Floyd’s triangle. It runs from 1 to n (inclusive).
- c represents the current row number.
9. Inner Loop (for columns in each row):
for (d = 1; d <= c; d++)
- This for loop governs how many elements are printed in a given row. How many elements in each row is equivalent to the row number (c).
- d is the column number for the row being printed at the moment.
10. Printing the Numbers:
System.out.print(num + ” “);
num++;
- In the inner loop, the program prints the current value of num followed by a space.
- After printing it increases the value of num by 1 so that the next number can be printed in the next iteration.
11. Starting a New Line for Each Row:
System.out.println();
After printing all numbers in the current row (i.e., when the inner loop completes), this line is executed to move to the next line for the next row.
12. End of Program:
Once the loops are finished, the program terminates, having printed Floyd’s triangle with the specified number of rows.
Example Walkthrough:
Let’s assume the user has entered 4 as the number of rows. The program will print Floyd’s Triangle with 4 rows.
Input
Enter the number of rows of floyd’s triangle you want
4
Output
Floyd’s triangle :-
1
2 3
4 5 6
7 8 9 10
Explanation of the Output:
- Row 1: The first row contains 1 number (1).
- Row 2: The second row contains 2 numbers (2, 3).
- Row 3: In row 3 there are 3 numbers, that are: 4, 5, 6.
- Row 4: In the fourth row, there are four numbers: 7, 8, 9, 10.
It can be easily noticed that all the numbers within the triangle form an ascending order from 1 to 10, while one more number fills each row about the row above it.
Important Information:
1. Floyd’s Triangle: It is a right-angled triangle with starting numbers being at 1 and continuing one after the other.
2. The outer loop deals with the no. of rows.
3. The inner loop deals with the no. of elements present in each row.
4. As we are in the cth row, there exists c no. of elements, this is why we are running from the first column to the last of any row. No. of elements are increasing.
Complexity:
- Time Complexity: O(n^2), due to the total number of numbers printed would be \begin{aligned}\\frac{n(n+1)}{2}\\end{aligned}, taking constant time for a number.
- Space Complexity: \\( O(1) \\), because it involves the use of very few variables and space is not a function of the input.