-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrimeNumber.java
More file actions
43 lines (39 loc) · 1.11 KB
/
PrimeNumber.java
File metadata and controls
43 lines (39 loc) · 1.11 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
package ClassPractice1;
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
/*int num = 6;
boolean flag = false;
for (int i = 2; i <= num/2; i++){
if(num % i == 0){
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");*/
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}