Triple Nested For Loop with specific output (java)

I need to write some java using 3 "for" loops that output

122333444455555

22333444455555

333444455555

444455555

55555

The code I have so far is:

public static void problemFour() {
      for(int i = 5; i >= 1; i--) {
         for(int a = 1; a <= i; a++) {
            for(int b = 1; b <= a; b++) {
               System.out.print(a);
            }
         }
         System.out.println();
      }
   }

Displays

111112222333445
11111222233344
111112222333
111112222
11111

I have included many keyboard shortcuts ++ - 's, <s,>, 5 and 1.

I'm pretty stuck if someone could point me in the right direction, that would be fantastic.

+4
source share
6 answers

You made a mistake in how the line starts and how many times the digit is repeated (here the symbol). Fix it:

for(int i = 1; i <= 5; i++) {          // Each iteration for one line
    for(int a = i; a <= 5; a++) {      // starts with a for ith line
        for(int b = 1; b <= a; b++) {  // a times `a` digit
            System.out.print(a);
        }
    }
    System.out.println();
}

To just your problem, first think about printing this template:

12345
2345
345
45
5

: , :

for(int b = 1; b <= a; b++) {  // a times `a` digit
     System.out.print(a);
}
+5

, 1 , 2 , 3 ..

firstValueInLine , ; number - , ; counter number number

    for (int firstValueInLine = 1; firstValueInLine <= 5; ++firstValueInLine) {
        for (int number = firstValueInLine; number <= 5; ++number) {
            for (int counter = 0; counter < number; ++counter) {
                System.out.print(number);
            }
        }
        System.out.println();
    }
+1

. .

  public static void problemFour() {
    for (int i = 1; i <= 5; i++) {
      for (int a = i; a <= 5; a++) {
        for (int b = 1; b <= a; b++) {
          System.out.print(a);
        }
      }
      System.out.println();
    }
  }

:

122333444455555
22333444455555
333444455555
444455555
55555
+1

1- , 2- . 3rd for-loop, . , , . .

public static void problemFour() {
      for(int i = 1; i <= 5; i++) {
         for(int a = i; a <= 5; a++) {
             for(int b=0;b<a;b++){
               System.out.print(a);  
             }                
         }
         System.out.println();
      }
   }
0

.

public static void main(String[] args) {
    for (int i = 5, j = 1; i >= 1; i--, j++) { // Introduce a new variable j 
        for (int a = j; a <= 5; a++) { // change a=1 to a=j & a<=i to a<=5
            for (int b = 1; b <= a; b++) {
                System.out.print(a);
            }
        }
        System.out.println();
    }
}

:

122333444455555
22333444455555
333444455555
444455555
55555
0

:

public static void main(String[] args) {

        // The first loop defines the number of rows.
        for(int i=1; i<=5; i++)
        {
            // The second loop defines which digits to be printed.
            for(int j=i; j<=5; j++)
            {
                // The third loop defines how many times each digit should be printed.
                for(int k=0 ; k<j; k++)
                {
                    System.out.print(j);
                }
            }
            System.out.println();
        }
    }

:

122333444455555
22333444455555
333444455555
444455555
55555
0

Source: https://habr.com/ru/post/1686738/


All Articles