-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactors.java
More file actions
33 lines (33 loc) · 827 Bytes
/
PrimeFactors.java
File metadata and controls
33 lines (33 loc) · 827 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
public class PrimeFactors {
public static void primeFactors(int n){
for(int i=2; i<=Math.sqrt(n);i++){
while(n%i==0){
System.out.print(i+" ");
n=n/i;
}
}
if(n>1){
System.out.print(n+" ");
}
}
public static boolean isPrime(int n){
if(n<=1){
return false;
}
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0)
return false;
}
return true;
}
public static void main(String args[]){
int n = 125;
primeFactors(n);
System.out.println();
if(isPrime(n)){
System.out.println(n + " is a prime number.");
}else{
System.out.println(n + " is not a prime number.");
}
}
}