public class FactorGenerator{
private int number;
private int i;
public FactorGenerator(int numberToFactor){
number = numberToFactor;
}
public int nextFactor(){
while(number % i == 0){
System.out.print((Math.round(i)) + " ");
number =((number / i));
return i;
}
return i;
}
public boolean hasMoreFactors(){
for ( i = 2; i <= number; i++){
nextFactor();
}
return false;
}
}
Test Program:
import java.util.Scanner;
public class FactorgeneratorTester{
public static void main (String [] args){
Scanner in= new Scanner(System.in);
System.out.println("input the value");
int number = in.nextInt();
FactorGenerator fg = new FactorGenerator(number);
if (fg.hasMoreFactors()){
System.out.println(fg.hasMoreFactors());
}
}
}
the input
210
output
2 3 5 7