-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckPrimeNo.java
More file actions
44 lines (40 loc) · 1.15 KB
/
CheckPrimeNo.java
File metadata and controls
44 lines (40 loc) · 1.15 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
36
37
38
39
40
41
42
43
44
// Print if a number n is prime or not (Input n from the user).
import java.util.*;
public class CheckPrimeNo {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
int temp = 0;
/*boolean isPrime = true;
for(int i=2; i<=n/2; i++) {
if(n % i == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
if(n == 1) {
System.out.println("This is neither prime not composite");
} else {
System.out.println("This is a prime number");
}
} else {
System.out.println("This is not a prime number");
}*/
if(n == 1){
System.out.println("Neihter prime not Composite");
} else{
for(int i=2; i<=n-1; i++) {
if(n % i == 0) {
temp += 1;
}
}
if(temp == 0){
System.out.println(n+" This number is prime");
}else{
System.out.println(n+"This number is not prime");
}
}
}
}