Friday , 28 July 2017
Home » Class 10 » Program to find the Prime Factors of a Number [Method 1]

Program to find the Prime Factors of a Number [Method 1]

Question:

Write a program in JAVA to find the Prime factors of a number.

Prime factors of a number are those factors which are prime in nature and by which the number itself is completely divisible (1 will not be taken as prime number).

Few such numbers are:
Prime Factors of 24 are 2, 2, 2, 3
Prime Factors of 6 are 2, 3

Solution:

/**
 * The class PrimeFactors inputs a number and prints all its PrimeFactors
 * @author : www.javaforschool.com
 * @Program Type : BlueJ Program - Java
 */

import java.io.*;
class PrimeFactors
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Enter a Number : ");
n=Integer.parseInt(br.readLine());

System.out.print("The Prime Factors of "+n+" are : ");

int i=2;
while(n>1)
  {
   if(n%i == 0)
    {
     System.out.print(i+" ");
     n=n/i;
    }
   else
    i++;
  }
}
}

Output:

Enter a Number : 378
The Prime Factors of 378 are: 2  3  3  3  7

Explanation:

In this program we have implemented the very traditional method of finding the prime factors of a number, which we all used in junior classes.

When we were told to find the HCF of 2 numbers, we first used to find the Prime Factors of both the numbers separately and then used to take the common factors and multiply them for finding the HCF – Remember?

And we used to first start dividing that number by 2, then if it was divisible, we would again divide it by 2 and this would go on till it is being divided by 2, then we moved on to check its divisibility by 3, then with 5, 7 etc. We used to divide, until we reached 1, after which we used to stop.

2 | 24  
2 | 12  
2 | 6    
3 | 3  
   | 1

Remember, the above method? And the Prime Factors using the above method would come as 2, 2, 2, 3 for 24.

This is the very logic being used in the above program.

1. We start dividing the number by 2, hence, the value of ‘i’ begins with 2.

2. If it is divisible, then the value of ‘i’ is printed, and the next number becomes (n/i) , like we used to have in our junior arithmetic.

3. If it is not divisible by 2, then the value of ‘i’ is incrementing.

4. And this checking goes on till the number is greater than 1, because if it becomes 1, we need to stop this process of dividing.

Check Also

[Question 1] ISC 2017 Computer Practical Paper Solved – Box Packing

Solution of Program 1 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input number of boxes to be packed and display the break-up of the cartons used in descending order of capacity

2 comments

  1. 2 | 24
    2 | 12
    2 | 6
    3 | 3
    | 1

    How to Print this type of Prime factor . plz give the code

    • import java.io.*;
      class PrimeFactors
      {
      public static void main() throws IOException
      {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      int n;
      System.out.print(“Enter a Number : “);
      n=Integer.parseInt(br.readLine());

      int i=2;
      while(n>1)
      {
      if(n%i == 0)
      {
      System.out.println(i+”t| “+n);
      n=n/i;
      }
      else
      i++;
      }
      System.out.println(“t| 1”);
      }
      }

Leave a Reply

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