-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeCheck.java
More file actions
47 lines (43 loc) · 1.03 KB
/
PrimeCheck.java
File metadata and controls
47 lines (43 loc) · 1.03 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
45
46
47
/*
* Program to check if a number is prime:
* Write a Java program to check if a given number is prime or not.
*/
import java.util.Scanner;
public class PrimeCheck
{
public static void primeCheck(int n)
{
boolean prime = true;
if(n==1 || n==0)
{
System.out.println(n+" is neither prime nor composite");
}
else
{
for(int i=2; i<=Math.sqrt(n); i++)
{
if((n%i)==0)
{
prime = false;
break;
}
}
if(prime)
{
System.out.println(n+" is a prime number");
}
else
{
System.out.println(n+" is NOT a prime number");
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int n = sc.nextInt();
sc.close();
primeCheck(n);
}
}