Question 59 : Write a program in Java to prime factorize a number taken as input from the user.
Java Program :
import java.io.*;
class prime_fact
{
static void check()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number : ");
int c=Integer.parseInt(br.readLine());
int i;
System.out.print("\nPrime Factors of "+c+" : ");
for(i=1;i<=c;i++)
{
if(c%i==0 && prime(i)==true)
{
System.out.print(i+" X ");
c=c/i;
i=1;
}
if(prime(c)==true)
{
System.out.print(c);
break;
}
}
}
static boolean prime(int a)
{
int i,j=0;
for(i=1;i<=a/2;i++)
{
if(a%i==0)
j++;
}
if(j==1)
return true;
else
return false;
}
}
No comments:
Post a Comment