Given a positive integer 'n', print the smallest number 'x' that is greater than or equal to 'n' which is a multiple of 3. Use a while loop to generate a sequence of natural numbers, starting from 'n', and then find the smallest number in this sequence that is a multiple of 3. Use the modulus (%) operator to check for a multiple of 3, if 'x' modulo 3 equals 0 then 'x' is a multiple of 3. Print -1 if 'n' is not a positive integer.
While and do-while loops
Finding the smallest multiple of 3 larger than 'n'
Report a typo
Sample Input 1:
5Sample Output 1:
6Sample Input 2:
10Sample Output 2:
12Write a program in Java 25
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get the input number
int n = sc.nextInt();
if (n > 0) {
// Write your code here. Use a while loop starting from n
// and check for the smallest multiple of 3.
} else {
System.out.println("-1");
}
sc.close();
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.