This Java program checks the divisibility of a number by 2, 3, 5, 7, or 11.
To understand this example, you should have the knowledge of the following Java programming topics:
A java program that checks wheather the User Given Number is Divisble by 2,3,5,7,11 is as follows
import java.util.Scanner;
public class DivisibilityChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("Choose divisibility checks:");
System.out.println("1. Check divisibility by 2");
System.out.println("2. Check divisibility by 3");
System.out.println("3. Check divisibility by 5");
System.out.println("4. Check divisibility by 7");
System.out.println("5. Check divisibility by 11");
System.out.println("Enter option (1-5): ");
int option = scanner.nextInt();
switch (option) {
case 1:
if (number % 2 == 0) {
System.out.println(number + " is divisible by 2.");
} else {
System.out.println(number + " is not divisible by 2.");
}
break;
case 2:
if (number % 3 == 0) {
System.out.println(number + " is divisible by 3.");
} else {
System.out.println(number + " is not divisible by 3.");
}
break;
case 3:
if (number % 5 == 0) {
System.out.println(number + " is divisible by 5.");
} else {
System.out.println(number + " is not divisible by 5.");
}
break;
case 4:
if (number % 7 == 0) {
System.out.println(number + " is divisible by 7.");
} else {
System.out.println(number + " is not divisible by 7.");
}
break;
case 5:
if (number % 11 == 0) {
System.out.println(number + " is divisible by 11.");
} else {
System.out.println(number + " is not divisible by 11.");
}
break;
default:
System.out.println("Invalid option. Please choose a number between 1 and 5.");
}
scanner.close();
}
}
Checking the divisbilty by 2
Enter a number: 14
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 1
14 is divisible by 2.
Checking the divisbilty by 3
Enter a number: 27
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 2
27 is divisible by 3.
Checking the divisbilty by 5
Enter a number: 45
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 3
45 is divisible by 5.
Checking the divisbilty by 11
Enter a number: 33
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 5
33 is divisible by 11.
Invalid Option
Enter a number: 18
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 6
Invalid option. Please choose a number between 1 and 5.
Don’t know how to take input from the user ? Look at this examples