Enter a number: 7
The number is Odd. The number is Prime.
import java.util.Scanner;
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
// Check Odd or Even
if (num % 2 == 0) {
System.out.println("The number is Even.");
} else {
System.out.println("The number is Odd.");
}
// Check Prime
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println("The number is Prime.");
} else {
System.out.println("The number is Not Prime.");
}
sc.close();
}
}
If num % 2 == 0 → Even, otherwise Odd.
For prime check:
Numbers ≤ 1 are not prime.
Check divisibility from 2 to num/2.
If divisible → Not Prime.
Otherwise → Prime.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.