Java Program To Print Floyd Triangle

Sharing is healthy !!

In the following post you will learn java program to print Floyd Triangle. Basically it is an algorithm, named after computer scientist Robert Floyd.

Floyd Triangle

Floyd Triangle is defined as right-angled triangular array of natural numbers consisting of numbers starting with 1 in the top left corner.

Floyd Triangle Example:

1
2 3
4 5 6
7 8 9 10

As you can see above numbers are filled in rows of triangle.

example: Java Program To Print Floyd Triangle

Java Program To Print Floyd Triangle

Also Read – Java Program To Find GCD And LCM Of Two Numbers

import java.util.Scanner;

public class Floyd_Triangle {

public static void main(String[] args) {

int x;
int number = 1;
int y;
int z;

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to print Floyd's Triangle: ");
x = sc.nextInt();
System.out.println("Floyd Triangle:");

for(y = 1;y <= x;y++){
for(z = 1;z <= y;z++){
System.out.println(number + " ");
number++;
}
System.out.println();
}
}
}

Output:

Enter the number of rows to print Floyd’s Triangle: 5
Floyd Triangle:

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

Sharing is healthy !!

Leave a Reply

Your email address will not be published.