Java Program To Print Prime Factors Of A Number

java program to print prime factors


Before jumping into coding and getting our hands dirty let's first understand concept of  Prime Factors.

Factors are the numbers you multiply to get another number. For instance, factors of 15 are 3 and 5, because 3×5 = 15.

Prime number: A number which has only two factors, i.e. 1 and the number itself.             

Example: 2, 3, 5, 7.. etc. 

So basically a prime factors of a number are the numberswhich are prime as well as it's factor.

Example: Prime Factors of 24 are 2 2 2 3

Steps to be followed for printing prime factors

  1. Input a number.
  2. Create a loop till ( number >0 )
  3. Take a variable let 'i' and initialise it to 2
  4. While number is divisible by i, print 'i' and divide number by 'i'.
  5. If unable to divide then increase value of 'i'
Example 1
Input:24
Output:2 2 2 3

Example 2
Input:63
Output:3 3 7

Sounds Good ? Let's get started

Code:


/**
 * @theschoolprogrammer
 * www.theschoolprogrammer.com
 */
public class PrimeFactors
{
    void main(int n)
    {
        int i=2,j;
        while(n>0)
        {
            if(n%i==0)
            {
                System.out.print(i+" ");
                n=n/i;
            }
            else
            {
                i++;
            }
        }
    }
}


Join Us

For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..




  • 
    /**
    *@theschoolprogrammer
    *www.theschoolprogrammer.com
    */
    import java.io.*;
    public class PrimeFactors
    {
      void main()throws IOException
      {
          
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("ENTER A NUMBER");
          int n=Integer.parseInt(br.readLine());
          int i=2,j;
          while(n>0)
          {
              if(n%i==0)
              {
                  System.out.println(i);
                  n=n/i;
              }
              else
              {
                  i++;
              }
          }
      }
    }
    
  • 
    /**
    *@theschoolprogrammer
    *www.theschoolprogrammer.com
    */
    import java.util.*;
    public class PrimeFactors
    {
    void main()
    {
    	
    	Scanner sc=new Scanner(System.in);
    	System.out.println("ENTER A NUMBER");
    	int n=sc.nextInt();
    	int i=2,j;
    	while(n>0)
    	{
    			if(n%i==0)
    			{
    					System.out.println(i);
    					n=n/i;
    			}
    			else
    			{
    					i++;
    			}
    	}
    }
    }
    


BACK


Java Introduction

For loops

While loops

Nested loops

Series And Sum of Series

Comments

  1. Amazing page design and content

    Loved it..

    Keep Going 💯💯👍👍

    ReplyDelete
  2. The code is write but what is the point of the variable j?

    ReplyDelete
You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send