# Prime factors in Java

In this post, you will learn a **Java program** to print **prime factors** of a number. Before starting the coding part, it should be clear about the prime number and prime factor.

A **prime number** is a whole number greater than 1, whose only factors are 1 and itself. The prime numbers, which divide the given number perfectly, are known as the prime factors of that number. Basically, it is finding prime numbers that prime numbers multiply together to make the original number. The prime factorization serves useful when working with big numbers, such as in Cryptography or it is a commonly used in mathematical problem to secure public-key encryption systems.

For example, if the input number is 110, then the output should be "2 5 11". And if the input number is 99, then the output should be "3 11". With the help of these two conditions, we can check whether a number is a prime factor of a given number or not-

- The number must be a prime number.
- The number must perfectly divide the given number.

## Java Program to find prime factors of a number

The given **Java** program finds the prime factors of a number using for loop. First, we create a function **isPrime()** to check whether the number is prime or not. This function returns 1 if a number is prime, otherwise returns 0. We create another function **primeFactors()** to find the prime factors. Inside this function, we run a loop from **2** to **num** and create a variable **x** to hold **num**. Next, we run a while loop that decrements and prints the value of **i** when **x** is divisible by **i**.

```
import java.util.Scanner;
import java.io.*;
import java.lang.Math;
class JavaPrimeFactors {
public static int isPrime(int num){
for(int i = 2; i<=Math.sqrt(num); i++){
if(num%i==0)
return 0;
}
return 1;
}
public static void primeFactors(int num)
{
for(int i = 2; i<= num; i++){
if(isPrime(i)==1){
int x = num;
while(x%i==0){
System.out.print(i + " ");
x /= i;
}
}
}
}
public static void main(String[] args)
{
// Getting integer value from user
Scanner scn = new Scanner(System.in);
System.out.print("Enter the number:");
int n = scn.nextInt();
primeFactors(n);
}
}
```

**Output of the above code:**

```
Enter the number:110
2 5 11
Enter the number:110
2 5 11
Enter the number:99
3 3 11
```

### Related Articles

**Number pattern programs in Java**

Java program to find area of rectangle

Matrix multiplication in Java

Electricity bill program in Java

Java program to find area of triangle

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert decimal to binary in Java

Convert decimal to octal in Java

Convert decimal to hexadecimal in Java

Simple interest program in Java

Check whether the given number is even or odd in java

Print prime numbers from 1 to 100 in Java

Java prime number program

Java program to convert celsius to fahrenheit

Fibonacci series program in Java

Java program to check leap year

Java program to find factorial of a number

Java program to find area of rectangle

Matrix multiplication in Java

Electricity bill program in Java

Java program to find area of triangle

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert decimal to binary in Java

Convert decimal to octal in Java

Convert decimal to hexadecimal in Java

Simple interest program in Java

Check whether the given number is even or odd in java

Print prime numbers from 1 to 100 in Java

Java prime number program

Java program to convert celsius to fahrenheit

Fibonacci series program in Java

Java program to check leap year

Java program to find factorial of a number