Java program to print Floyd’s triangle

In this post we are going to 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 java program to print Floyd’s triangle,

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