-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumber.java
More file actions
30 lines (30 loc) · 850 Bytes
/
Copy pathPrimeNumber.java
File metadata and controls
30 lines (30 loc) · 850 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
29
30
import java.util.*;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number ::");
int num = sc.nextInt();
boolean flag = false;
// for loop approach
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
// while loop approach
int j = 2;
while(j <= num){
if (num % j == 0) {
flag = true;
break;
}
++j;
}
// print the result
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}