Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:
Floyd’s Triangle Algorithm:
Start
Declare and initialize required variables for controlling loop, inputting number of rows and printing numbers.
Enter the number of rows to be printed.
Print the number in standard format utilizing the application of loop as follows
do for x=1 to n
do for y=1 to n
print number
increase the number ans y by 1
go to next line
Print triangle
Stop
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int numberOfRows;
System.out.print("Enter the number of rows : ");
Scanner in = new Scanner(System.in);
numberOfRows = in.nextInt();
int number = 1;
for (int row = 1; row <= numberOfRows; row++)
{
for (int column = 1; column <= row; column++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}
Output:
The Tech Platform
Commentaires