Prime factors in java


Factors are the numbers we multiply to get another number.

factors of 14 are 2 and 7, because 2 × 7 = 14.

Some numbers can be factored in more than one way.

16 can be factored as 1 × 16, 2 × 8, or 4 × 4.

A number that can only be factored as 1 times itself is called a prime number.

The first few primes are 2, 3, 5, 7, 11, and 13.

The list of all the prime-number factors of a given number is the prime factors of a number. The factorization of a number into its prime factors and expression of the number as a product of its prime factors is known as the prime factorization of that number. The prime factorization of a number includes ONLY the prime factors, not any products of those prime factors.

Program

import java.util.Scanner;

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

Output

Enter a number
24
2
2
2
3

Updated on: 06-Nov-2023

23K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements