> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Nested Loops

> 9 assignments

## 119. Nesting Loops - [Link](https://programmingbydoing.com/a/nesting-loops.html)

<Tabs>
  <Tab title="Task">
    In programming, the term "nested" usually means to put something inside the same thing. "Nested loops" would be two loops with one inside the other one. If you do it right, then means the inner loop will repeat *all* its iterations every time the outer loop does *one* more iteration.

    Start by downloading the following code, and get it to compile.

    **Files Needed**

    * [NestingLoops.java](https://programmingbydoing.com/a/examples/NestingLoops.java)

    **Sample Output**

    ```bash theme={null}
    A 1
    A 2
    A 3
    B 1
    B 2
    B 3
    C 1
    C 2
    C 3
    D 1
    D 2
    D 3
    E 1
    E 2
    A 3

    1-1 1-2 1-3 2-1 2-2 2-3 3-1 3-2 3-3
    ```
  </Tab>

  <Tab title="Code">
    ```java NestingLoops.java theme={null}
    public class NestingLoops
    {
        public static void main( String[] args )
        {
            // this is #1 - I'll call it "CN"
            for ( char c='A'; c <= 'E'; c++ )
            {
                for ( int n=1; n <= 3; n++ )
                {
                    System.out.println( c + " " + n );
                }
            }

            System.out.println("\n");

            // this is #2 - I'll call it "AB"
            for ( int a=1; a <= 3; a++ )
            {
                for ( int b=1; b <= 3; b++ )
                {
                    System.out.print( a + "-" + b + " " );
                }
            }

            System.out.println();
        }
    }
    ```
  </Tab>

  <Tab title="Question">
    Assignments turned in *without* these things will not receive any points.

    1. Look at the first set of nested loops ("CN"). Which variable changes faster? Is it the variable controlled by the outer loop (c) or the variable controlled by the inner loop (n)? Answer in a comment.
       > Answer: The variable controlled by the inner loop (n) changes faster.
    2. Change the order of the loops so that the "c" loop is on the inside and the "n" loop is on the outside. How does the output change?

       > Answer: The output will change to

           <CodeGroup>
             ```java Change theme={null}
             for ( int n=1; n <= 3; n++ )
             {
                 for ( char c='A'; c <= 'E'; c++ )
                 {
                     System.out.println( n + " " + c );
                 }
             }
             ```

             ```bash Output theme={null}
             1 A
             1 B
             1 C
             1 D
             1 E
             2 A
             2 B
             2 C
             2 D
             2 E
             3 A
             3 B
             3 C
             3 D
             3 E
             ```
           </CodeGroup>
    3. Look at the second set of nested loops ("AB"). Change the print() statement to println(). How does the output change? (Then change it back to print().)

       > Answer: The output will change to

           <CodeGroup>
             ```java Change theme={null}
             for ( int a=1; a <= 3; a++ )
             {
                 for ( int b=1; b <= 3; b++ )
                 {
                     System.out.println( a + "-" + b );
                 }
             }
             ```

             ```bash Output theme={null}
             1-1
             1-2
             1-3
             2-1
             2-2
             2-3
             3-1
             3-2
             3-3
             ```
           </CodeGroup>
    4. Add a System.out.println() statement after the close brace of the inner loop (the "b" loop), but still inside the outer loop. How does the output change?

       > Answer: The output will change to

           <CodeGroup>
             ```java Change theme={null}
             for ( int a=1; a <= 3; a++ )
             {
                 for ( int b=1; b <= 3; b++ )
                 {
                     System.out.print( a + "-" + b + " " );
                 }
                 System.out.println();
             }
             ```

             ```bash Output theme={null}
             1-1 1-2 1-3
             2-1 2-2 2-3
             3-1 3-2 3-3
             ```
           </CodeGroup>
  </Tab>
</Tabs>

## 120. Odometer Loops - [Link](https://programmingbydoing.com/a/odometer-loops.html)

<Tabs>
  <Tab title="Task">
    Download the following code, and get it to compile.

    **Files Needed**

    * [OdometerLoops.java](https://programmingbydoing.com/a/examples/OdometerLoops.java)

    **Sample Output**

    ```bash theme={null}
    9999
    ```

    (It looks a little cooler in person.)
  </Tab>

  <Tab title="Code">
    ```java OdometerLoops.java theme={null}
    public class OdometerLoops
    {
        public static void main( String[] args ) throws Exception
        {
            for ( int thous=0; thous<10; thous++ )
            {
                for ( int hund=0; hund<10; hund++ )
                {
                    for ( int tens=0; tens<10; tens++ )
                    {
                        for ( int ones=0; ones<10; ones++ )
                        {
                            System.out.print( " " + thous + "" + hund + "" + tens + "" + ones + "\r" );
                            Thread.sleep(10);
                        }
                    }
                }
            }

            System.out.println();
        }
    }
    ```
  </Tab>

  <Tab title="Question">
    Assignments turned in *without* these things will not receive any points.

    1. Delete all the open braces and close braces from all the outer `for` loops. (Leave the curly braces that belong to the innermost loop (the "ones" loop).) Does it still work? Answer in a comment.
       > Answer: Yes, it still works.
       ```java Change theme={null}
       for ( int thous=0; thous<10; thous++ )
           for ( int hund=0; hund<10; hund++ )
               for ( int tens=0; tens<10; tens++ )
                   for ( int ones=0; ones<10; ones++ )
                   {
                       System.out.print( " " + thous + "" + hund + "" + tens + "" + ones + "\r" );
                       Thread.sleep(10);
                   }
       ```
    2. Change all the loops so that they count from 0 to 7 instead of 0 to 9. This will display numbers in "octal" (base 8) instead of "decimal" (base 10).

       > Answer: The output will change to

           <CodeGroup>
             ```java Change theme={null}
             for ( int thous=0; thous<8; thous++ )
                 for ( int hund=0; hund<8; hund++ )
                     for ( int tens=0; tens<8; tens++ )
                         for ( int ones=0; ones<8; ones++ )
                         {
                             System.out.print( " " + thous + "" + hund + "" + tens + "" + ones + "\r" );
                             Thread.sleep(10);
                         }
             ```

             ```bash Output theme={null}
             7777
             ```
           </CodeGroup>
    3. Change the code so that the human gets to type in a number for the base, and your odometer counts up to that instead of 8. You might want to increase the delay (put a bigger number (like maybe 500) inside the ). `Thread.sleep()`

       > Answer:

           <CodeGroup>
             ```java Change theme={null}
             import java.util.Scanner;

             public class OdometerLoops
             {
                 public static void main( String[] args ) throws Exception
                 {
                     Scanner keyboard = new Scanner(System.in);
                     System.out.print("Which base (2-10): ");
                     int base = keyboard.nextInt();

                     for ( int thous=0; thous<base; thous++ )
                         for ( int hund=0; hund<base; hund++ )
                             for ( int tens=0; tens<base; tens++ )
                                 for ( int ones=0; ones<base; ones++ )
                                 {
                                     System.out.print( " " + thous + "" + hund + "" + tens + "" + ones + "\r" );
                                     Thread.sleep(500);
                                 }

                     System.out.println();
                 }
             }
             ```

             ```bash Output theme={null}
             Which base (2-10): 5
             4444
             ```
           </CodeGroup>

    After you've made all the changes, it should look something like this (except that all your numbers will be overwriting each other on the same line instead of printing on separate lines):

    ```bash theme={null}
    Which base (2-10): 2
    0000
    0001
    0010
    0011
    0100
    0101
    ```
  </Tab>
</Tabs>

## 121. Basic Nested Loops - [Link](https://programmingbydoing.com/a/basic-nested-loops.html)

<Tabs>
  <Tab title="Task">
    Use some simple nested `for` loops to generate all possible coordinates from (0,0) up to (5,5).

    For full credit, your output *must* appear in rows and columns like mine.

    **Sample Output**

    ```bash theme={null}
    (0,0) (0,1) (0,2) (0,3) (0,4) (0,5)
    (1,0) (1,1) (1,2) (1,3) (1,4) (1,5)
    (2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
    (3,0) (3,1) (3,2) (3,3) (3,4) (3,5)
    (4,0) (4,1) (4,2) (4,3) (4,4) (4,5)
    (5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
    ```
  </Tab>

  <Tab title="Code">
    ```java BasicNestedLoops.java theme={null}
    public class BasicNestedLoops
    {
        public static void main( String[] args )
        {
            for ( int i=0; i<=5; i++ )
            {
                for ( int j=0; j<=5; j++ )
                {
                    System.out.printf( "(%s,%s) ", i, j );
                }
                System.out.println();
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 122. Multiplication Table - [Link](https://programmingbydoing.com/a/multiplication-table.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to generate a multiplication table, which should go all the way up to 12x9 (if you use tabs, 12x12 won't fit on one screen).

    It is hard to make the table look much nicer than the one below.

    **Sample Output**

    ```bash theme={null}
    x | 1   2       3       4       5       6       7       8       9
    ==+================================================================
    1 | 1   2       3       4       5       6       7       8       9
    2 | 2   4       6       8       10      12      14      16      18
    3 | 3   6       9       12      15      18      21      24      27
    4 | 4   8       12      16      20      24      28      32      36
    5 | 5   10      15      20      25      30      35      40      45
    6 | 6   12      18      24      30      36      42      48      54
    7 | 7   14      21      28      35      42      49      56      63
    8 | 8   16      24      32      40      48      56      64      72
    9 | 9   18      27      36      45      54      63      72      81
    10 | 10 20      30      40      50      60      70      80      90
    11 | 11 22      33      44      55      66      77      88      99
    12 | 12 24      36      48      60      72      84      96      108
    ```
  </Tab>

  <Tab title="Code">
    ```java MultiplicationTable.java theme={null}
    public class MultiplicationTable
    {
        public static void main( String[] args )
        {
            System.out.println( "x | 1\t2\t3\t4\t5\t6\t7\t8\t9" );
            System.out.println( "==+==================================================================" );

            for ( int i=1; i<=12; i++ )
            {
                System.out.printf( "%s | ", i );
                for ( int j=1; j<=9; j++ )
                {
                    System.out.printf( "%s\t", i*j );
                }
                System.out.println();
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 123. Number Puzzles I - [Link](https://programmingbydoing.com/a/number-puzzle-i.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to generate a list of all the pairs of positive two digit numbers whose sum is 60, and whose difference is 14.

    **Sample Output**

    ```bash theme={null}
    Perulangan dari 10 - 99
    37 + 23 = 60 dan 37 - 23 = 14
    ```
  </Tab>

  <Tab title="Code">
    ```java NumberPuzzles1.java theme={null}
    public class NumberPuzzles1
    {
        public static void main( String[] args )
        {
            for ( int i=10; i<=99; i++ )
            {
                for ( int j=10; j<=99; j++ )
                {
                    if ( (i+j==60) && (i-j==14) )
                    {
                        System.out.printf( "%s + %s = %s dan %s - %s = %s\n", i, j, i+j, i, j, i-j );
                    }
                }
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 124. Getting Individual Digits - [Link](https://programmingbydoing.com/a/getting-individual-digits.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to generate a list of all the positive two digit numbers. Display the numbers, and the sums of their digits.

    **Sample Output**

    ```bash theme={null}
    10, 1+0 = 1
    11, 1+1 = 2
    12, 1+2 = 3
    13, 1+3 = 4
    14, 1+4 = 5
    15, 1+5 = 6
    16, 1+6 = 7
    17, 1+7 = 8
    18, 1+8 = 9
    19, 1+9 = 10
    20, 2+0 = 2
    21, 2+1 = 3
    22, 2+2 = 4

    // (etc.)

    97, 9+7 = 16
    98, 9+8 = 17
    99, 9+9 = 18
    ```
  </Tab>

  <Tab title="Code">
    ```java GettingIndividualDigits.java theme={null}
    public class GettingIndividualDigits
    {
        public static void main( String[] args )
        {
            for ( int i=1; i<=9; i++ )
            {
                for ( int j=0; j<=9; j++ )
                {
                    System.out.printf( "%s%s, %s+%s = %s\n", i, j, i, j, i+j );
                }
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 125. More Number Puzzles - [Link](https://programmingbydoing.com/a/more-number-puzzles.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to generate a list of all the two digit numbers which are less than or equal to fifty-six, and the sum of whose digits is greater than ten.

    Use another set of nested `for` loops to find a two-digit number such that the number itself minus the number reversed is equal to the sum of its digits.

    For example, 72 is not such a number because 72-27 (which is 45) is **not** the same as the sum of its digits (2+7 = 9).

    Finally, put the code for each of the two parts into its own separate function, and have a menu in `main()` which allows you to choose which of the two sets to find. This main program should keep repeating until you choose to quit (use a `do-while` loop for this).

    **Sample Output**

    ```bash theme={null}
    1) Find two digit numbers <= 56 with sums of digits > 10
    2) Find two digit number minus number reversed which equals sum of digits
    3) Quit

    >1

    (numbers go here)

    1) Find two digit numbers <= 56 with sums of digits > 10
    2) Find two digit number minus number reversed which equals sum of digits
    3) Quit

    >2

    (number goes here)

    1) Find two digit numbers <= 56 with sums of digits > 10
    2) Find two digit number minus number reversed which equals sum of digits
    3) Quit

    >3
    ```
  </Tab>

  <Tab title="Code">
    ```java NumberPuzzles2.java theme={null}
    import java.util.Scanner;

    public class NumberPuzzles2
    {
        public static void main( String[] args )
        {
            Scanner keyboard = new Scanner(System.in);
            int choice;

            do
            {
                System.out.println( "\n1) Find two digit numbers <= 56 with sums of digits > 10" );
                System.out.println( "2) Find two digit number minus number reversed which equals sum of digits" );
                System.out.println( "3) Quit" );
                System.out.print( "\n> " );
                choice = keyboard.nextInt();

                System.out.println();

                if ( choice == 1 )
                {
                    findTwoDigitNumbers();
                }
                else if ( choice == 2 )
                {
                    findTwoDigitNumber();
                }
            } while ( choice != 3 );
        }

        public static void findTwoDigitNumbers()
        {
            for ( int i=1; i<=5; i++ )
            {
                for ( int j=0; j<=9; j++ )
                {
                    if ( i != 5 && j != 6 )
                    {
                        if ( i+j > 10 )
                        {
                            System.out.printf( "%s%s, %s+%s = %s\n", i, j, i, j, i+j );
                        }
                    }
                }
            }
        }

        public static void findTwoDigitNumber()
        {
            for ( int i=1; i<=9; i++ )
            {
                for ( int j=0; j<=9; j++ )
                {
                    if ( (i*10+j) - (j*10+i) == i+j )
                    {
                        System.out.printf( "%s%s, %s%s - %s%s = %s\n", i, j, i, j, j, i, i+j );
                    }
                }
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 126. Number Puzzles III: Armstrong Numbers - [Link](https://programmingbydoing.com/a/number-puzzles-iii.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to find all the three-digit Armstrong numbers. Armstrong numbers are three digit numbers such that the sum of the digits cubed is equal to the number itself.

    For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 153

    However, 294 is not, because 2³ + 9³ + 4³ = 801 (not 294)
  </Tab>

  <Tab title="Code">
    ```java ArmstrongNumbers.java theme={null}
    public class ArmstrongNumbers
    {
        public static void main( String[] args )
        {
            for ( int i=1; i<=9; i++ )
            {
                for ( int j=0; j<=9; j++ )
                {
                    for ( int k=0; k<=9; k++ )
                    {
                        if ( (i*100+j*10+k) == (Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3)) )
                        {
                            System.out.printf( "%s^3 + %s^3 %s^3 = %s\n", i, j, k, (int)(Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3)) );
                        }
                    }
                }
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 127. Number Puzzles IV: A New Hope - [Link](https://programmingbydoing.com/a/number-puzzles-iv-a-new-hope.html)

<Tabs>
  <Tab title="Task">
    Use nested `for` loops to find four positive integers whose sum is 45, and such that the first plus 2, the second minus 2, the third multiplied by 2, and the fourth divided by 2 are all equal.

    Please note that these four numbers are **integers**, and not necessarily just digits. In this problem, we are no longer finding a single four-digit number; we are finding four separate numbers. However, since we are trying to generate all possible combinations, the procedure should be the same.

    For those of you having trouble with the English on this one (I've noticed that students really don't like word problems), here's what that means.

    * The output of your program should be four numbers. They won't be negative numbers.
    * When you add the four numbers, they add up to 45.
    * If you add 2 to the first number, you get the same answer as if you had subtracted 2 from the second number.
    * If you multiply the third number by two, you also get the same answer.
    * If you divide the fourth number by two, you also get the same answer.

    The following mathematical statments are also true about these numbers:

    $~~~~~A + 2 = B - 2 = C \times 2 = D \div 2 $

    and

    $~~~~~A + B + C + D = 45 $
  </Tab>

  <Tab title="Code">
    ```java NumberPuzzles4.java theme={null}
    public class NumberPuzzles4
    {
        public static void main( String[] args )
        {
            for ( int a=1; a<=9; a++ )
            {
                for ( int b=0; b<=9; b++ )
                {
                    for ( int c=0; c<=9; c++ )
                    {
                        for ( int d=0; d<=9; d++ )
                        {
                            if ( (a+b+c+d == 45) && (a+2 == b-2) && (b-2 == c*2) && (c*2 == d/2) )
                            {
                                System.out.printf( "%s + %s + %s + %s = 45\n", a, b, c, d );
                            }
                        }
                    }
                }
            }
        }
    }
    ```
  </Tab>
</Tabs>
