-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime_of_Number.java
40 lines (36 loc) · 1.12 KB
/
Prime_of_Number.java
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
// Num = 30 => 1,2,3,5,6,10,15,30 --> [2,3,5]
// Num = 35 => 1,5,7,35 -->[5,7]
import java.util.Scanner;
public class Prime_of_Number {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number: ");
int number = sc.nextInt();
printPrimeDivisors(number);
}
public static void printPrimeDivisors(int number) {
System.out.println("Prime divisors of " + number + " are:");
for (int i = 2; i <= Math.sqrt(number); i++) {
while (number % i == 0) {
if (isPrime(i)) {
System.out.print(i + " ");
}
number /= i;
}
}
if (number > 2 && isPrime(number)) {
System.out.print(number);
}
}
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}