Java
Java

 

Introduction:

Prime factorization is the process of determining the prime numbers that multiply together to form a given number. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. For example, the prime factorization of 18 is 2 × 3 × 3.

Objective:

The objective of this program is to find the prime factors of a given number using Java programming language. The program will take an integer input from the user, and output its prime factors.

Java Program to Find Prime Factors of a Number


import java.util.Scanner;

public class PrimeFactorization {

    // Method to print prime factors of a number
    public static void primeFactors(int n) {
        // Divide out the number 2 until it's no longer divisible
        while (n % 2 == 0) {
            System.out.print(2 + " ");
            n /= 2;
        }
        
        // Check for odd factors from 3 upwards
        for (int i = 3; i <= Math.sqrt(n); i += 2) { // Divide the number by i until it's no longer divisible while (n % i == 0) { System.out.print(i + " "); n /= i; } } // If the number is a prime number greater than 2 if (n > 2) {
            System.out.print(n);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter a number
        System.out.print("Enter a number to find its prime factors: ");
        int number = scanner.nextInt();
        
        System.out.println("Prime factors of " + number + " are:");
        primeFactors(number);
        
        scanner.close();
    }
}

Explanation of the Program

This Java program consists of two main components:

  • primeFactors(int n): This method is used to find and print the prime factors of the given number n.
  • main(String[] args): This is the main method of the program where we accept user input and call the primeFactors method to display the results.

Program Flow:

  1. The program first takes an integer input from the user.
  2. The primeFactors method is called with the user-provided number.
  3. Inside the primeFactors method, the number is repeatedly divided by 2 until it’s no longer divisible by 2 (removing all factors of 2).
  4. Then, it checks for all odd numbers starting from 3 up to the square root of the number and divides out the factors until the number is reduced.
  5. Finally, if the remaining number is greater than 2, it is printed as a prime factor.

How to Run the Program:

To run this program, follow these steps:

    1. Step 1: Copy the Java code provided above into a text file and save it as PrimeFactorization.java.
    2. Step 2: Open your command prompt or terminal and navigate to the directory where your Java file is saved.
    3. Step 3: Compile the Java program by typing the following command:
javac PrimeFactorization.java
    1. Step 4: Once compiled, run the program by typing the following command:
java PrimeFactorization
  1. Step 5: Enter an integer when prompted to see the prime factors of the number.

Example Run:

    Enter a number to find its prime factors: 60
    Prime factors of 60 are:
    2 2 3 5 

This program efficiently finds and displays the prime factors of any number provided as input. It makes use of simple mathematical principles of division and checking divisibility.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)