-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprime.java
More file actions
28 lines (26 loc) · 796 Bytes
/
prime.java
File metadata and controls
28 lines (26 loc) · 796 Bytes
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
//this program input from user and identifies whether the number is prime or non-prime
import java.util.*;
public class prime {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int num;
System.out.println("Enter a number: ");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
if (isPrime(num)) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
}
}