ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Prime Factor Calculator Java Program

Updated on February 10, 2013

Furthermore, Prime Factors are factors of a number, which only compose of prime numbers. To check if the prime factors of a number is correct, the product of the factors should be equal to the number itself.

For example:

Look for the prime factor of 20:

Possible factors for 20 are:

2, 5, 2

10, 2

1, 20

The correct prime factors of 20 are 2, 5, 2 because they are all prime numbers opposite to 10, 2 and 1, 20.

Another Example:

Look for the Prime Factors of 50:

Possible Factor:

50, 1

10, 5

2, 5, 5

Correct Prime Factors are: 2, 5, 5

You can experiment to look for prime factors by running the below Java Program to your Java Development kit.

Prime factor calculator Java Program

Prime numbers are numbers that are only divisible by itself like 2, 3, 5, 7, 13 and so on. Note that Prime numbers are different to odd numbers. All the prime numbers are odd numbers but not all odd numbers are prime numbers.

What is the Program Algorithm?

First it will check if the number is an even number by assigning a variable i to 2,then continue to check it as it increment by 1. If it is an even number, it will continuously divide the number to the incrementing variable i until the number is not divisible to any number equal or less than to the half of the entered number. This is to make sure that the numbers are all prime numbers. See the sample code below and run it for better understanding.

Sample Source Code for Prime Factor Calculator

package primefactor;

import java.util.Scanner;

public class Main {

    public static void main(String[] args)
       int num;
       Scanner input = new Scanner(;

       System.out.print("Enter a number: ");
       num = input.nextInt();

       System.out.print("Prime Factor/s of "+ num + ": ");

           for(int i=2; i<=num; i++)
               if(num%i == 0)
                 num = num/i;
                 System.out.print(i + " ");


Sample Output:


    0 of 8192 characters used
    Post Comment

    No comments yet.