The reason that is happening is because after you find out 5 is a factor of x, you are dividing x by 5, and then incrementing i to 6. This is where it is broken, because the new value of x is also divisible by 5.
You can solve this very easily, by checking if it is still divisible by i, and if so, decrementing i, before it gets incremented, so it will check that value of i again.
Here is the code you originally posted, with a slight modification:
Code :
import java.util.Scanner;
public class Factor {
public static void main(String[] args){
Scanner input=new Scanner (System.in);
String yn;
int number;
int current;
String output = "";
int x;
int i = 2;
System.out.print("Enter a number to factor : ");
number = input.nextInt();
while (number % 2 == 0)
{
output += 2 +" ";
number /= 2;
}
x = number;
while (i <= x)
{
[B]if (number % i == 0)[/B]
{
output += i + " ";
x /= i;
//I added from here
if(x % i == 0){
i--;
}
//to here
}
i++;
}
System.out.println("Factors are : " + output);
}
}
There are still problems with this code by the way. I bolded the problem area.... I'll leave it up to you to fix it (should be a very obvious and quick fix.)