Java program to find all prime factors of a number

In Java, to find all prime factors of a number we can use loop concepts such as while loop or for loop.

Prime number is number that is divisible by 1 and the number itself. For a given number, we should find all the prime factors (Exactly divides the number). If the input number is 6, then the output should be 2,3.

In this article we will learn through an example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We have to write a java program to print all prime factors of a number. The program should take one integer number as input and should check for all the prime factors for that number. This action will be done in our programs. Let’s see an example,

Example:

For input integer number 1020, Output should be 2 2 3 5 17

Explanation:

Input Integer number:  1020

Factors of 1020:

1,2,3,4,5,6,10,12,15,17,20,30,34,51,60,68,85,102,170,204,255,340,510,1020.

Prime factors of 1020: 2 2 3 5 17

Hence, for integer number 1020, Prime factors are 2 2 3 5 17.

Logic to find all the prime factors of a number

  • Take an input number from the user.
  • Use a outer loop (while or for loop) to iterate through potential factors starting from 2 (i (factor) =2) up to number-1.
  • Inside the loop, use a while loop to check number is divisible by 2 (number%i==0).
  • If true, it means i is a prime factor of number. So, print i.
  • Update number by dividing it by i (number= number/i).
  • Repeat this process until number is no longer divisible by i.
  • After exiting the outer loop (while or for loop), check if a number is greater than 2 using if statement.
  • If yes, it means number itself is a prime number and should be printed as the last prime factor.
  • Prime factors are printed one by one as they are found during the iteration and close the scanner class.

Java program to find all the prime factors of a number

Program 1: Using while loop

In the below program we are using while loop to print all the prime factors of a given number in Java.

import java.util.Scanner;
public class PrimeFactors {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a number");
		int number = s.nextInt();
		int i = 2;
		System.out.println("Prime Factors Are: ");
		while (i < number) {
			while (number % i == 0) {
				number = number / i;
				System.out.println(i);
			}
			i++;
		}
		if(number>2) {
			System.out.println(number);
		}
		s.close();
	}
}

Output:

Enter a number
315
Prime Factors Are:
3
3
5
7

Java program explanation to find all the prime factors of a number

  1. The above java program imports the Scanner class from java.util package for user input.
  2. In the main method:
    • It creates a Scanner object to read user input.
    • Asks the user to enter a number.
    • Reads the integer input from the user.
    • Initializes a variable ‘i’ to 2.
    • Enters a while loop that continues as long as ‘i’ is less than the input number.
    • Inside the loop:
      • Checks if ‘i’ is a factor of the input number.
      • If ‘i’ is a factor, it repeatedly divides the input number by ‘i’ while it remains divisible, printing ‘i’ each time.
      • Increments ‘i’ by 1.
    • After the loop, if the remaining input number is greater than 2, it implies that it’s a prime factor, so it gets printed.
    • Closes the Scanner object to release system resources.

Program 2: Using for loop

In the below program we are using for loop to print all the prime factors of a given number in Java.

import java.util.Scanner;
public class PrimeFactors {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a number");
		int number = s.nextInt();
		System.out.println("Prime Factors Are: ");
		for (int i = 2; i < number; i++) {
			while (number % i == 0) {
				number = number / i;
				System.out.println(i);
			}
		}
		if (number > 2) {
			System.out.println(number);
		}
		s.close();
	}
}

Output:

Enter a number
1268
Prime Factors Are:
2
2
317

Java program explanation to find all the prime factors of a number

  1. The above java program imports the Scanner class from java.util package for user input.
  2. In the main method:
    • It creates a Scanner object to read user input.
    • Asks the user to enter a number.
    • Reads the integer input from the user.
    • Enters a for loop that iterates from 2 up to (number – 1).
    • Inside the loop:
      • Checks if the current iteration variable ‘i’ is a factor of the input number.
      • If ‘i’ is a factor, it divides the input number by ‘i’ repeatedly while it remains divisible, printing ‘i’ each time.
    • After the loop, if the input number is greater than 2, it implies that the remaining number after factorization is also a prime factor, so it gets printed.
    • Closes the Scanner object to release system resources.

Conclusion

In this article, we have learnt how to print prime factors of a given number in Java. We have seen the logic and understood the program using for loop in Java and while loop in Java. Hope this article helped you in understanding the program.

You can also check our another awesome articles

Happy Coding!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.