> ## 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.

# Loops

<Tabs>
  <Tab title="Sample 1">
    Write a program to print integers from 1 to 6

    **Output:**\
    1\
    2\
    3\
    4\
    5\
    6
  </Tab>

  <Tab title="Solution">
    ```java theme={null}
        public class Main {
            public static void main(String[] args) {
                for (int i = 1; i <= 6; i++) {
                    System.out.println(i);
                }
            }
        }
    ```
  </Tab>

  <Tab title="Sample 2">
    Write a program to display n first Fibonacci numbers.

    **Sample Input:**\
    7

    **Sample Output:**\
    1 1 2 3 5 8 13
  </Tab>

  <Tab title="Solution">
    ```java theme={null}
        public class Main {
            public static void main(String[] args) {
                int n = 7, first = 0, second = 1;
                for (int i = 1; i <= n; i++) {
                    System.out.print(second + " ");
                    int next = first + second;
                    first = second;
                    second = next;
                }
            }
        }
    ```
  </Tab>

  <Tab title="Sample 3">
    Write a program to display all factors of the number that the user has entered. Factors are the numbers you multiply together to get another number.

    Example: All the factors of 12

    * 2 × 6 = 12,
    * but also 3 × 4 = 12,
    * and of course 1 × 12 = 12.
    * So **1, 2, 3, 4, 6 and 12** are factors of 12.

    **Sample Input:**\
    12\
    **Sample Output:**\
    1 2 3 4 6 12
  </Tab>

  <Tab title="Solution">
    ```java theme={null}
        public class Main {
            public static void main(String[] args) {
                int n = 12;
                for (int i = 1; i <= n; i++) {
                    if (n % i == 0) {
                        System.out.print(i + " ");
                    }
                }
            }
        }
    ```
  </Tab>
</Tabs>

# `for` Loop

<Tabs>
  <Tab title="Example">
    ```java theme={null}
      for (initialization; condition; update statement) {
          // do something
      }

      for (int i = 1; i <= 10; i++) {
          System.out.println(i);
      }
      for (int i = 0; i < 10; i++) {
          System.out.println(i + 1);
      }
    ```
  </Tab>

  <Tab title="Flowchart">
    ```java theme={null}
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum = sum + i;
    }
    System.out.println(sum);
    ```

    ![Flowchart of for loop](https://i.ibb.co/CBDHsxj/unnamed.png)
  </Tab>
</Tabs>

### Example `for` Loop

<Tabs>
  <Tab title="Example 1">
    ```java theme={null}
    int n = 5;
    for (int i = 1, j = n; i <= n && j > 0; i++, j--) {
        System.out.println(i + " - " + j);
    }

    Output:
    1 - 5
    2 - 4
    3 - 3
    4 - 2
    5 - 1
    ```
  </Tab>

  <Tab title="Example 2">
    ```java theme={null}
    int n = 5, i = 1, j = n;
    for (; i <= n && j > 0;) {
        System.out.println(i + " - " + j);
        i++;
        j--;
    }

    Output:
    1 - 5
    2 - 4
    3 - 3
    4 - 2
    5 - 1
    ```
  </Tab>

  <Tab title="Example 3">
    ```java theme={null}
    for (;;) {
        System.out.println("Hello");
    }

    Output:
    Hello
    Hello
    Hello
    Hello
    ...
    ```
  </Tab>
</Tabs>

### Exercise `for` Loop

<Tabs>
  <Tab title="Exercise 1">
    Write a program using for loop to print odd integers from 1 to n (inclusive).

    **Sample input:**\
    10

    **Sample output:**\
    1 3 5 7 9
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    import java.util.Scanner;
    public class OddNumbers {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for (int i = 1; i <= n; i += 2) {
                System.out.print(i + " ");
            }
        }
    }
    ```
  </Tab>

  <Tab title="Exercise 2">
    Write a program using `for` loop to determine whether the number entered by the user is a
    prime number or not.\
    Prime numbers are positive integers which are only divisible by 1 or the number itself.
    Examples: 2, 3, 5, 7, ...

    **Sample input:**\
    5\
    **Sample output:**\
    Prime

    **Sample input:**\
    10\
    **Sample output:**
    Not Prime
  </Tab>

  <Tab title="Solution Exercise 2">
    ```java theme={null}
    import java.util.Scanner;
    public class PrimeNumber {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            boolean isPrime = true;
            for (int i = 2; i <= n / 2; i++) {
                if (n % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.println("Prime");
            } else {
                System.out.println("Not Prime");
            }
        }
    }
    ```
  </Tab>

  <Tab title="Exercise 3">
    The following code should display the character "\*" 42 times. However, the programmer who
    wrote it made a mistake.

    ```java theme={null}
        int n = 42;
        for (int i = 0; i < n; i--) {
            System.out.print("*");
        }
    ```

    Fix the code using only one of the following ways:

    1. Add one character
    2. Delete one character
    3. Replace one character with another character
  </Tab>

  <Tab title="Solution Exercise 3">
    ```java theme={null}
    int n = 42;
    for (int i = 0; i < n; i++) {
        System.out.print("*");
    }
    ```
  </Tab>
</Tabs>

# `while` Loop

<Tabs>
  <Tab title="Example">
    ```java theme={null}
      while (condition) {
          // do something
      }

      int n = 1;
      while (n <= 5) {
          System.out.println(n);
          n++;
      }
    ```
  </Tab>

  <Tab title="Flowchart">
    ```java theme={null}
    int i = 5, sum = 0;
    while (i > 0) {
        sum = sum + i;
        i--;
    }
    System.out.println(sum);
    ```

    ![Flowchart of for while](https://i.ibb.co/8dNjnNj/Screenshot-2024-04-03-180604.png)
  </Tab>
</Tabs>

### Exercise `while` Loop

<Tabs>
  <Tab title="Exercise 1">
    Write a program using while loop to print integers from n to 1.

    **Sample input:**
    10

    **Sample output:**
    10 9 8 7 6 5 4 3 2 1
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    import java.util.Scanner;
    public class PrintNumbers {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            while (n > 0) {
                System.out.print(n + " ");
                n--;
            }
        }
    }
    ```
  </Tab>

  <Tab title="Exercise 2">
    Write a program using while loop to calculate the average of several numbers entered by the user. The number of inputs from users varies. The input will end if the user enters -1.

    **Sample input:**
    10\
    8\
    9\
    6\
    -1\\

    **Sample output:**
    8.25
  </Tab>

  <Tab title="Solution Exercise 2">
    ```java theme={null}
    import java.util.Scanner;
    public class Average {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int sum = 0, count = 0;
            while (true) {
                int n = sc.nextInt();
                if (n == -1) {
                    break;
                }
                sum += n;
                count++;
            }
            System.out.println((double) sum / count);
        }
    }
    ```
  </Tab>
</Tabs>

# `do-while` Loop

<Tabs>
  <Tab title="Example">
    ```java theme={null}
      do {
          // do something
      } while (condition);

      int n = 10;
      do {
          System.out.println(n);
          n--;
      } while (n > 0);
    ```
  </Tab>

  <Tab title="Flowchart">
    ```java theme={null}
    int i = 5, sum = 0;
    do {
        sum = sum + i;
        i--;
    } while (i > 0);
    System.out.println(sum);
    ```

    ![Flowchart of for do-while](https://i.ibb.co/ft0CKNp/Screenshot-2024-04-03-181041.png)
  </Tab>
</Tabs>

### Example `do-while` Loop

```java theme={null}
double number;
do {
    System.out.print("Enter a positive number: ");
    number = input.nextDouble();
} while (number <= 0);
System.out.println("You entered " + number);
```

### Exercise `do-while` Loop

<Tabs>
  <Tab title="Exercise 1">
    Write a program using do-while loop to display the multiplication table of numbers entered by the user.

    **Sample input:**\
    5

    **Sample output:**\
    5 x 1 = 5\
    5 x 2 = 10\
    5 x 3 = 15\
    5 x 4 = 20\
    5 x 5 = 25\
    5 x 6 = 30\
    5 x 7 = 35\
    5 x 8 = 40\
    5 x 9 = 45\
    5 x 10 = 50\\
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    import java.util.Scanner;
    public class MultiplicationTable {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int i = 1;
            do {
                System.out.println(n + " x " + i + " = " + n * i);
                i++;
            } while (i <= 10);
        }
    }
    ```
  </Tab>
</Tabs>

# `Nested` Loops

<Tabs>
  <Tab title="Sample 1">
    ```java theme={null}
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            // do something
        }
    }
    ```
  </Tab>

  <Tab title="Sample 2">
    ```java theme={null}
    boolean ok = false;

    while (!ok) {
        for (int i = 0; i < 10; i++) {
            // do something
            if (condition) {
                ok = true;
            }
            // ....
        }
    }
    ```
  </Tab>

  <Tab title="Sample 3">
    ```java theme={null}
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            System.out.println(i + "-" + j);
        }
    }
    ```
  </Tab>
</Tabs>

### Exercise `Nested` Loops

<AccordionGroup>
  <Accordion title="Exercise 1 Rectangle">
    <Tabs>
      <Tab title="Question">
        Make a program to display m × n rectangles formed with star characters (\*). m is the length of the rectangle, and n is its width.

        **Sample Input:**\
        7 3

        **Sample Output:**\
        \* \* \* \* \* \* \* \
        \* \* \* \* \* \* \* \
        \* \* \* \* \* \* \*
      </Tab>

      <Tab title="Solution">
        ```java theme={null}
        import java.util.Scanner;
        public class Rectangle {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int m = sc.nextInt();
                int n = sc.nextInt();
                for (int i = 0; i < m; i++) {
                    for (int j = 0; j < n; j++) {
                        System.out.print("* ");
                    }
                    System.out.println();
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 2 NumericTriangle">
    <Tabs>
      <Tab title="Question">
        Write a program to display a numeric triangle illustrated as follows.

        **Sample Input:**\
        5

        **Sample Output:**\
        1\
        12\
        123\
        1234\
        12345
      </Tab>

      <Tab title="Solution">
        ```java theme={null}
        import java.util.Scanner;
        public class NumericTriangle {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= i; j++) {
                        System.out.print(j);
                    }
                    System.out.println();
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 3 NumericTriangle2">
    <Tabs>
      <Tab title="Question">
        Write a program to display a numeric triangle illustrated as follows.

        **Sample Input:**\
        5

        **Sample Output:**\
        $~~~~~~~$ 1\
        $~~~~~$ 12\
        $~~~$ 123\
        $~$ 1234\
        12345
      </Tab>

      <Tab title="Solution">
        ```java theme={null}
        import java.util.Scanner;
        public class NumericTriangle {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n - i; j++) {
                        System.out.print(" ");
                    }
                    for (int j = 1; j <= i; j++) {
                        System.out.print(j);
                    }
                    System.out.println();
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 4 NumericTriangle3">
    <Tabs>
      <Tab title="Question">
        Write a program to display a numeric triangle illustrated as follows.

        **Sample Input:**\
        5

        **Sample Output:**\
        $~~~~~~~$ 1\
        $~~~~~$ 121\
        $~~~$ 12321\
        $~$ 1234321\
        123454321
      </Tab>

      <Tab title="Solution">
        ```java theme={null}
        import java.util.Scanner;
        public class NumericTriangle {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n - i; j++) {
                        System.out.print(" ");
                    }
                    for (int j = 1; j <= i; j++) {
                        System.out.print(j);
                    }
                    for (int j = i - 1; j >= 1; j--) {
                        System.out.print(j);
                    }
                    System.out.println();
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 5 NumericTriangle4">
    <Tabs>
      <Tab title="Question">
        Write a program to display a numeric triangle illustrated as follows.

        **Sample Input:**\
        n = 5

        **Sample Output:**\
        $~~~~~~~$ 1\
        $~~~~~$ 121\
        $~~~$ 12321\
        $~$ 1234321\
        123454321\
        $~$ 1234321\
        $~~~$ 12321\
        $~~~~~$ 121\
        $~~~~~~~$ 1
      </Tab>

      <Tab title="Solution">
        ```java theme={null}
        import java.util.Scanner;
        public class NumericTriangle {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);

                System.out.print("n: ");
                int n = sc.nextInt();

                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n - i; j++) {
                        System.out.print(" ");
                    }

                    for (int j = 1; j <= i; j++) {
                        System.out.printf("%s", j % 10);
                    }

                    for (int j = i - 1; j >= 1; j--) {
                        System.out.printf("%s", j % 10);
                    }
                    System.out.println();
                }

                for (int i = n - 1; i >= 1; i--) {
                    for (int j = 1; j <= n - i; j++) {
                        System.out.print(" ");
                    }

                    for (int j = 1; j <= i; j++) {
                        System.out.printf("%s", j % 10);
                    }

                    for (int j = i - 1; j >= 1; j--) {
                        System.out.printf("%s", j % 10);
                    }
                    System.out.println();
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>
</AccordionGroup>

# `break` dan `continue`

```java theme={null}
for (int i = 0; i < 10; i++) {
    if (i == 2) {
        continue; #To the next iteration
    } else if (i == 7) {
        break;    #Exit Loop
    }
    System.out.println(i);
}
```

### Using `break`

<CodeGroup>
  ```java before theme={null}
  Scanner in = new Scanner(System.in);
  boolean ok = false;
  while (!ok) {
      System.out.print("Enter a number: ");
      if (in.hasNextDouble()) {
          ok = true;
      } else {
          String word = in.next();
          System.out.println(word + " is not a number");
      }
  }
  double x = in.nextDouble();
  ```

  ```java after theme={null}
  Scanner in = new Scanner(System.in);
  while (true) {
      System.out.print("Enter a number: ");
      if (in.hasNextDouble()) {
          break;
      }
      String word = in.next();
      System.out.println(word + " is not a number");
  }
  double x = in.nextDouble();
  ```
</CodeGroup>

### Using `continue`

```java theme={null}
Scanner in = new Scanner(System.in);
int x = -1;
int sum = 0;
while (x != 0) {
    x = in.nextInt();
    if (x <= 0) {
        continue;
    }
    System.out.println("Adding " + x);
    sum += x;
}
System.out.println(sum);
```

### Exercise `break` dan `continue`

<Tabs>
  <Tab title="Exercise 1">
    By using break or continue, write a program to determine whether the number entered by the user is a prime number or not.

    **Sample Input:**\
    5

    **Sample Output:**\
    Prime
  </Tab>

  <Tab title="Solution Exercise 1">
    ```java theme={null}
    import java.util.Scanner;
    public class PrimeNumber {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            boolean isPrime = true;
            for (int i = 2; i <= n / 2; i++) {
                if (n % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.println("Prime");
            } else {
                System.out.println("Not Prime");
            }
        }
    }
    ```
  </Tab>

  <Tab title="Exercise 2">
    By using break or continue, write a program to determine the Greatest Common Divisor (GCD) of the two numbers entered by the user.

    **Sample Input:**\
    25 20

    **Sample Output:**\
    5
  </Tab>

  <Tab title="Solution Exercise 2">
    ```java theme={null}
    import java.util.Scanner;
    public class GCD {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            int b = sc.nextInt();
            int gcd = 1;
            for (int i = 1; i <= a && i <= b; i++) {
                if (a % i == 0 && b % i == 0) {
                    gcd = i;
                }
            }
            System.out.println(gcd);
        }
    }
    ```
  </Tab>
</Tabs>

### Tambahan Materi `Nested` Loops di kelas

<AccordionGroup>
  <Accordion title="Exercise 1 Digit Sum Sequence">
    <Tabs>
      <Tab title="Question">
        Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.

        **Sample Input:**\
        Masukkan angka: = 12345

        **Sample Output:**\
        5 4 3 2 1\
        sum of digits: = 15
      </Tab>

      <Tab title="Solution">
        ```java DigitSumSequence.java theme={null}
        import java.util.Scanner;
        public class DigitSumSequence {
            public static void main(String[] args) {
                Scanner input = new Scanner(System.in);

                System.out.print("Masukkan angka: ");
                int number = input.nextInt();

                int sum = 0;
                do {
                    int lastDigit = number % 10;
                    System.out.printf("%d ", lastDigit);
                    sum += lastDigit;
                    number /= 10;
                } while (number > 0);

                System.out.println("\nsum of digits: " + sum);
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 2 Nested Digit Sum Sequence one digit">
    <Tabs>
      <Tab title="Question">
        Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.

        **Sample Input:**\
        Masukkan angka: = 12345

        **Sample Output:**\
        5 4 3 2 1\
        sum of digits: = 15\
        5 1\
        sum of digits: = 6\\
      </Tab>

      <Tab title="Solution">
        ```java NestedDigitSumSequence.java theme={null}
        import java.util.Scanner;
        public class NestedDigitSumSequence {
            public static void main(String[] args) {
                Scanner input = new Scanner(System.in);

                System.out.print("Masukkan angka: ");
                int number = input.nextInt();

                int sum = 0;
                do {
                    int temp = number;
                    sum = 0;
                    while (temp > 0) {
                        int lastDigit = temp % 10;
                        System.out.printf("%d ", lastDigit);
                        sum += lastDigit;
                        temp /= 10;
                    }
                    System.out.println();
                    System.out.println("sum of digits: " + sum);
                    number = sum;
                } while (sum > 9);
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 3 Fibonacci Custom Sum">
    <Tabs>
      <Tab title="Question">
        **Sample Output:**\
        1 1 2 3 5 8 13 12 7 10 8 9 17 17 16 15 13 10 5 6 11 ...
      </Tab>

      <Tab title="Solution">
        ```java FibonacciCustomSum.java theme={null}
        public class FibonacciCustomSum {
            public static void main(String[] args) {
                int a = 1;
                int b = 1;

                System.out.print(a + " " + b + " ");

                for (int i = 3; i <= 1000; i++) {
                    int sum = digitSum(a) + digitSum(b);
                    System.out.print(sum + " ");
                    a = b;
                    b = sum;
                }
            }

            public static int digitSum(int digit) {
                int number = digit;
                int sum = 0;
                while (number > 0) {
                    int lastDigit = number % 10;
                    sum += lastDigit;
                    number /= 10;
                }
                return sum;
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>

  <Accordion title="Exercise 4 Check Digit Sum">
    <Tabs>
      <Tab title="Question">
        Status Exercise `Progress`

        **Sample Input:**\
        Masukkan angka: = 123\
        Masukkan angka: = 456\
        Masukkan angka: = 451

        **Sample Output:**\
        1 + 2 = 3 (memenuhi syarat)\
        4 + 5 = 6 (tidak memenuhi syarat)\
        4 + 1 = 5 (memenuhi syarat)
      </Tab>

      <Tab title="Solution">
        ```java CheckDigitSum.java theme={null}
        import java.util.Scanner;
        public class CheckDigitSum {
            public static void main(String[] args) {
                Scanner input = new Scanner(System.in);

                System.out.print("Masukkan angka: ");
                int n = input.nextInt();

                int number = n;
                int max = 0;
                int sum = 0;
                while (number > 0) {
                    int lastDigit = number % 10;
                    sum += lastDigit;
                    max = Math.max(max, lastDigit);
                    number /= 10;
                }

                if (sum == max * 2) {
                    System.out.printf("%d digits (memenuhi syarat)%n", n);
                } else {
                    System.out.printf("%d digits (tidak memenuhi syarat)%n", n);
                }
            }
        }
        ```
      </Tab>
    </Tabs>
  </Accordion>
</AccordionGroup>
