Prime Factor Constructor

BombHagel

New Member
I am making a two classes, the constructing class and the main method where I read a number from a user input and spits out the prime factorizations of the number, code is with java.For Example:
Enter Number: 150
5
5
3
2
However, for my program I'm getting the entire list of factors.Example:
Enter Number: 150
150
75
50
25
5
3
1
How would I change this up to get prime factors?Main Method:\[code\]import java.util.*;public class FactorPrinter{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a integer: "); String input1 = scan.nextLine(); int input = Integer.parseInt(input1); FactorGenerator factor = new FactorGenerator(input); System.out.print(factor.getNextFactor()); while (!factor.hasMoreFactors()) { System.out.print(factor.getNextFactor()); } } }\[/code\]Here is my class:\[code\]public class FactorGenerator { private int num; private int nextFactor; public FactorGenerator(int n) { num = nextFactor = n; } public int getNextFactor() { int i = nextFactor - 1 ; while ((num % i) != 0) { i--; } nextFactor = i; return i; } public boolean hasMoreFactors() { if (nextFactor == 1) { return false; } else { return true; } }}\[/code\]Help would be greatly appreciated. Thank you!
 
Back
Top