-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckForPrime.java
More file actions
48 lines (36 loc) · 894 Bytes
/
CheckForPrime.java
File metadata and controls
48 lines (36 loc) · 894 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package numerics;
import java.util.Scanner;
public class CheckForPrime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num,flag = 0;
System.out.println("Enter an Positive Integer : ");
num = sc.nextInt();
for (int i = 2; i <= num / 2; ++i) {
// condition for non-prime
if (num % i == 0) {
flag = 1;
break;
}
}
if (num == 1) {
System.out.println("1 is neither prime nor composite.");
}
else {
if (flag == 0)
System.out.println(num+" is a PRIME number");
else
System.out.println(num+" is NON-PRIME number");
}
}
}
/*
Output-1 :
Enter an Integer :
13
13 is a PRIME number
Output-2 :
Enter an Positive Integer :
22
22 is NON-PRIME number
*/