Finding the smallest multiple of 3 larger than 'n'

Report a typo

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.

Sample Input 1:

5

Sample Output 1:

6

Sample Input 2:

10

Sample Output 2:

12
Write 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