Java program to print Floyd’s triangle

Let’s learn java program to print Floyd’s triangle.

java program to print Floyd's triangle

Java program to print Floyd’s triangle

Floyd triangle in java is one of the common interview question. Floyd triangle is a right angled triangular array of natural numbers starting with 1 in the top left corner.

Also read – gcd and lcm of two numbers

In floyd triangle there are total (n(n + 1)) / 2 integers where “n” is integers in nth row.

1
2 3
4 5 6
7 8 9 10

Now let’s see how to print floyd’s triangle in java with example,

import java.util.Scanner;

public class FloydTriangleDemo
{
   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 : ");
      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 : 5
Floyd Triangle :

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