-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeOrNot.java
More file actions
35 lines (29 loc) · 1.01 KB
/
Copy pathPrimeOrNot.java
File metadata and controls
35 lines (29 loc) · 1.01 KB
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
32
33
34
35
package loops;
import java.util.Scanner;
public class PrimeOrNot {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n <= 1) {
System.out.println(n + " is not a prime number");
} else if (n == 2) {
System.out.println(n + " is a prime number");
} else {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) { // // n is a multiple of i (i is not equal to 1 or n)
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(n + " is a prime number");
} else {
System.out.println(n + " is not a prime number");
}
}
sc.close();
}
}
// Check if a number is prime or not (a prime number is a number that is only divisible by 1 and itself)