Use this file to discover all available pages before exploring further.
Sample 1
Solution
Sample 2
Solution
Sample 3
Solution
Write a program to print integers from 1 to 6Output:
1
2
3
4
5
6
public class Main { public static void main(String[] args) { for (int i = 1; i <= 6; i++) { System.out.println(i); } } }
Write a program to display n first Fibonacci numbers.Sample Input:
7Sample Output:
1 1 2 3 5 8 13
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; } } }
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
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 + " "); } } } }
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); }
int sum = 0;for (int i = 0; i < 5; i++) { sum = sum + i;}System.out.println(sum);
Write a program using for loop to print odd integers from 1 to n (inclusive).Sample input:
10Sample output:
1 3 5 7 9
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 + " "); } }}
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:
PrimeSample input:
10 Sample output:
Not Prime
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"); } }}
The following code should display the character ”*” 42 times. However, the programmer who
wrote it made a mistake.
int n = 42; for (int i = 0; i < n; i--) { System.out.print("*"); }
Fix the code using only one of the following ways:
Add one character
Delete one character
Replace one character with another character
int n = 42;for (int i = 0; i < n; i++) { System.out.print("*");}
Write a program using while loop to print integers from n to 1.Sample input:
10Sample output:
10 9 8 7 6 5 4 3 2 1
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--; } }}
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
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); }}
Write a program using do-while loop to display the multiplication table of numbers entered by the user.Sample input:
5Sample 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\
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); }}
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 3Sample Output:
* * * * * * *
* * * * * * *
* * * * * * *
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(); } }}
Exercise 2 NumericTriangle
Question
Solution
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5Sample Output:
1
12
123
1234
12345
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(); } }}
Exercise 3 NumericTriangle2
Question
Solution
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5Sample Output: 1 12 123 1234
12345
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(); } }}
Exercise 4 NumericTriangle3
Question
Solution
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5Sample Output: 1 121 12321 1234321
123454321
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(); } }}
Exercise 5 NumericTriangle4
Question
Solution
Write a program to display a numeric triangle illustrated as follows.Sample Input:
n = 5Sample Output: 1 121 12321 1234321
123454321 1234321 12321 121 1
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(); } }}
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();
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);
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:
5Sample Output:
Prime
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"); } }}
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 20Sample Output:
5
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); }}
Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345Sample Output:
5 4 3 2 1
sum of digits: = 15
DigitSumSequence.java
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); }}
Exercise 2 Nested Digit Sum Sequence one digit
Question
Solution
Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345Sample Output:
5 4 3 2 1
sum of digits: = 15
5 1
sum of digits: = 6\
NestedDigitSumSequence.java
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); }}
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; }}
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); } }}