What is Prime Factorization?
Every natural number can be expressed as a product of prime numbers.
Such a product representation is called prime factorization.
For Example:
300 = 2 ^ 2 * 3 ^ 1 * 5 ^ 2
580 = 2 ^ 2 * 5 ^ 1 * 29 ^ 1
Problem Statement:
Write a program to display the prime factorization of a given number in Java
import java.util.Scanner;
public class PrimeFactorization
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(
"*** Prime Factorization ***\n");
// Enter an integer :
int num = in.nextInt();
System.out.print(
" The prime factorization\n\n "
+" of number "+ num +" --> ");
// For each potential factor.
// (factor == fac)
for(int fac=2; (fac*fac)<=num; fac++) {
while(num%fac == 0) {
System.out.print(fac +" ");
num /= fac;
}
}
// If biggest factor occurs
// only once, num > 1.
System.out.print(
num > 1 ? num : "");
}
}