forked from Vivek-Py/LearnJava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckPrime.java
32 lines (24 loc) · 906 Bytes
/
CheckPrime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
public class CheckPrime {
public static void main(String[] args) {
int num, i, flag = 0;
//Input from the user
System.out.println("Enter a number to check if it is prime or not :");
Scanner scan = new Scanner(System.in);
num = scan.nextInt();
//Code to check if the input number is a prime number or not
for (i = 1; i < num / 2; i++) {
if (num % i == 0) {
flag++;
break;
}
}
if (num == 1 || num == 0 || num < 0) {
System.out.println("The number " + num + " is neither a prime nor a composite number.");
} else if (flag == 0) {
System.out.println("The number " + num + " is a prime number.");
} else {
System.out.println("The number " + num + " is not a prime number.");
}
}
}