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
import java.util.*;
class PrimeFactors
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter a Number : ");
n=sc.nextInt();
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
Few such numbers are:
Prime Factors of 24 are 2, 2, 2, 3
Prime Factors of 6 are 2, 3
import java.util.*;
class PrimeFactors
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter a Number : ");
n=sc.nextInt();
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
No comments:
Post a Comment