Assignment: Module 03- Part 02
Write a class PrimeFinder to accept the number and find it is a prime number or not? Using control structures
Input:
Enter the number 4
Output:
The given no is not a prime
Input:
Enter the number 17
Output:
The given no is prime
Here's one possible implementation in Java:
public class PrimeFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = scanner.nextInt();
if (num <= 1) {
System.out.println("The given no is not a prime");
} else {
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("The given no is prime");
} else {
System.out.println("The given no is not a prime");
}
}
}
}