-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPrimeNumber.java
More file actions
41 lines (32 loc) · 1.15 KB
/
PrimeNumber.java
File metadata and controls
41 lines (32 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
import java.util.Scanner;
/*
A prime number is a whole number greater than 1 whose only factors are 1 and itself.
A factor is a whole number that can be divided evenly into another number.
The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Numbers that have more than two factors are called composite numbers
*/
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//taking user input
System.out.print("Enter number to find its prime : ");
int n = sc.nextInt();
//calling the Findprime and out put the results
if(Findprime(n)==true){
System.out.println(n+" isn't a prime number");
}else{
System.out.println(n+" is a prime number");
}
sc.close();
}
// we can't find input number is prime by direct what we doing here is search they have other factors than 1 and input number.
public static boolean Findprime(int y){
int temp=y/2;
for(int i=2;i<temp;i++){
if((y%i)==0){
return true;
}
}
return false;
}
}