import java.util.*;
public class primeFactor{
public static void main(String args[]){
int integer;
Scanner scanner = new Scanner(System.in);
List<Integer> factors = new ArrayList<Integer>();
try{
System.out.print("\n\n\nEnter Integer : ");
integer = scanner.nextInt();
if(integer==0){
System.out.println("Error:Can not find primes for 0");
System.exit(-1);
}
System.out.print(integer+" = ");
for (Integer integer1 : FindSmallestFactors(integer)) {
System.out.print(integer1+" ");
}
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static List<Integer> FindSmallestFactors(int integer){
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= integer; i++) {
while (integer % i == 0) {
factors.add(i);
integer = integer / i;
}
}
return factors;
}
}
Thursday, February 28, 2013
Find Prime Factors Of Given Integer Using Java
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment