Floyd’s triangle number pattern using while loop in java

Let’s learn Floyd’s triangle number pattern using while loop in java.

Floyd’s triangle number pattern using while loop in java

Here let’s learn how to print floyd’s triangle in java using while loop. Let’s see java program.

import java.util.Scanner;
public class FloydTriangleWhileLoop
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want to print: ");
      int rows = sc.nextInt();
      int number = 1;
      System.out.println("Floyd triangle in java using while loop");
      int a = 1;
      int b = 1;
      while(a <= rows)
      {
         b = 1;
         while(b <= a)
         {
            System.out.println(number + " ");
            number++;
            b++;
         }
         System.out.println();
         a++;
      }
      sc.close();
   }
}


Output:

Enter the number of rows of floyd’s triangle you want to print: 4
Floyd triangle in java using while loop
1
2 3
4 5 6
7 8 9 10

Enter the number of rows of floyd’s triangle you want to print: 6
Floyd triangle in java using while loop
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21