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 is the program to print floyd’s triangle number pattern using nested while loop. Let’s see floyd’s triangle number pattern.

public class FloydTriangleWhileLoop
{
   public static void main(String[] args) 
   {
      int a = 1;
      while(a <= 10)
      {
         int b = 1;
         while(b <= a)
         {
            System.out.print(b + " ");
            b++;
         }
         System.out.println();
         a++;
      }
   }
}

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10


Let’s another floyd’s triangle number pattern.

public class FloydTriangleWhileLoop
{
   public static void main(String[] args) 
   {
      int a = 1;
      while(a <= 10)
      {
         int b = 1;
         while(b <= a)
         {
            System.out.print(a + " ");
            b++;
         }
         System.out.println();
         a++;
      }
   }
}

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10


Now here’s another floyd’s triangle number pattern.

public class FloydTriangleWhileLoop
{
   public static void main(String[] args) 
   {
      int a = 1, b, c, num1 = 10, num2 = 10, num = 0;
      while(a <= num1)
      {
         c = 1;
         while(c < num1 - (num1 - a))
         {
            System.out.print(" ");
            c++;
         }
         num = num2 - a;
         b = 1;
         while(b <= num)
         {
            System.out.print(num1 - (num1 - b));
            b++;
         }
         a++;
         System.out.println(" ");
      }
   }
}

Output:

123456789
12345678
1234567
123456
12345
1234
123
12
1