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
Answers
Answer:
import java.io.*;
import java.lang.Math;
class GFG
{
// A function to print all prime factors
// of a given number n
public static void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n%2==0)
{
System.out.print(2 + " ");
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
System.out.print(i + " ");
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
System.out.print(n);
}
public static void main (String[] args)
{
int n = 315;
primeFactors(n);
}
}
JAVA :-
import java.util.*;
class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,f=2;
System.out.println(“Enter a number:”);
n=sc.nextInt();
for(i=n;i>1;)
{
if(i%f==0)
{
System.out.println(f);
i=i/f;
}
else
f++;
}
}
}