Java >> Java tutorial >  >> Tag >> while

Floyds trekant-talmønster ved hjælp af while-løkke i java

Lad os lære Floyds trekanttalsmønster ved hjælp af while-løkke i java.

Floyds trekanttalsmønster ved hjælp af while-løkke i java

Her er programmet til at udskrive floyds trekanttalsmønster ved hjælp af indlejret while-løkke. Lad os se Floyds trekant-talmønster.

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

Lad os et andet floyd's trekanttalsmønster.

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

Her er et andet Floyds trekanttalsmønster.

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


Java tag