Tuesday, February 12, 2019

To find the prime factors of a number

import java.util.*;
class PrimeFactors
{
 
 public static void main()
 {  Scanner sc=new Scanner(System.in);
   int n; int c=0;
   System.out.println("Enter the number");
   n=sc.nextInt();
   System.out.println("Prime factors of "+n);
   for(int i=2;i<=n;i++)
   {
        while (n%i==0)
        {
          for(int j=2;j<=i/2;j++)
          {
            if(i%j==0)
             c++;
            }
            if(c==0)
            {
                System.out.println(i+" ");
                n=n/i;
            }
            c=0;
        }}
    }
}

No comments:

Post a Comment