Skip to content

Commit

Permalink
Create PrimeFactors.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tezivindh authored and x0lg0n committed Nov 1, 2024
1 parent e574b7c commit 7818c39
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Java/PrimeFactors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Scanner;

public class PrimeFactors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();

System.out.print("Prime factors of " + number + ": ");
findPrimeFactors(number);

scanner.close();
}

public static void findPrimeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}

for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}

if (n > 2) {
System.out.print(n);
}
}
}

0 comments on commit 7818c39

Please sign in to comment.