# How to find prime factors of a number in java

In this post, we will see how to find prime factors of a number in java.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
The prime factors of a number are all of the prime numbers that will exactly divide the given number.
For example-
Prime factor of 15 = 3,5
Prime factor of 48=2,2,2,2,3Lets create java program for it:
Run above program and you will get following output:
You must be wondering we are not checking whether loop variable i is prime or not But you don’t need to do this because in any loop, number has been already divided by 2 to i-1 so i can only be divisor if it is prime.

#### More optimized solution:

Change above primeFactors function to below function

This is based on the fact that in above for loop,divisor can not be greater than n/i.

Please go through java interview programs for more such programs.

import_contacts

## Related Posts

• 05 April

### Count occurrences of Character in String in Java

Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 FeaturesWas this post helpful? In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given […]

• 24 December

### How to capitalize first letter in java

Learn about how to capitalize first letter in java.

• 09 October

### Java program to find first and last digit of a number

Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() methodWas this post helpful? In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and […]

• 03 October

### Happy Number program in Java

Table of ContentsWhat is a Happy Number?Using HashsetAlgorithmExampleUsing slow and fast pointersAlgorithmExampleWas this post helpful? In this article, we are going to learn to find Happy Number using Java. Let’s first understand, what is Happy Number? What is a Happy Number? A number which leaves 1 as a result after a sequence of steps and […]

• 30 September

### Find Perfect Number in Java

Table of ContentsIterative approachRecursive approachWas this post helpful? In this article, we are going to find whether a number is perfect or not using Java. A number is called a perfect number if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches […]

• 28 September

### How to find Magic Number in Java

Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic NumberWas this post helpful? In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a […]