I have this code to find the smallest factors of any number that is inputted but I need to know how to get it to print an interger only once. ex. if I put in 120 I get 2,2,2,3,5. But I only the output to show 2,3,5 someone please help!!
import javax.swing.JOptionPane;
public class Exercise4_16 {
// Main method
public static void main(String args[]) {
// Prompt the user to enter a positive integer
String intString = JOptionPane.showInputDialog(
"Enter a positive integer:");
// Convert string to int
int number = Integer.parseInt(intString);
// Find all the smallest factors of the integer
System.out.println("The factors for " + number + " is");
int factor = 2;
while (factor <= number) {
if (number % factor == 0) {
number = number / factor;
System.out.println(factor);
}
else {
factor++;
}
}
}
}
2007-02-14
15:42:39
·
2 answers
·
asked by
Fresh Java meat
1
in
Computers & Internet
➔ Programming & Design