Cuchulainn
Cuchulainn

Reputation: 49

Triple Nested For Loop with specific output (java)

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

122333444455555

22333444455555

333444455555

444455555

55555

The code I have so far:

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();
      }
   }

This outputs

111112222333445
11111222233344
111112222333
111112222
11111

I've switched around a lot of combinations of ++'s --'s, <'s, >'s, 5's, and 1's.

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

Upvotes: 4

Views: 1773

Answers (5)

prabushitha
prabushitha

Reputation: 1483

Thing here is, use 1st for-loop for number of lines, 2nd for-loop for getting the number to print. 3rd for-loop to print the displaying number for the required amount. What you've done is correct in opposite way. So make your code reverse.

Try this

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();
      }
   }

Upvotes: 0

Saurav Sahu
Saurav Sahu

Reputation: 13994

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

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 simply your problem, first think about printing this pattern :

12345
2345
345
45
5

Then extend it: in innermost loop put the code for repetition equal to digit times, using :

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

Upvotes: 5

hirakJS
hirakJS

Reputation: 109

You can try the following. It will work.

  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();
    }
  }

Output:

122333444455555
22333444455555
333444455555
444455555
55555

Upvotes: 1

Damith
Damith

Reputation: 437

Checkout the inline comments.

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();
    }
}

Output:

122333444455555
22333444455555
333444455555
444455555
55555

Upvotes: 0

Adam Kotwasinski
Adam Kotwasinski

Reputation: 4564

We can use the observation that number 1 is printed only once, 2 twice, 3 thrice, etc.

firstValueInLine keeps the number the line starts; number is the number in line being printed; counter just ensures that number is printed number times

    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();
    }

Upvotes: 1

Related Questions