Can not see why this Prime factorization is not working

Here's my code:

import java.util.*;

public class FactorsOfAnInteger {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

int input = 0;

System.out.println("Enter an integer: ");

input = console.nextInt();

for (int i = 2; i < input; i++) {

while (input % i == 0){

System.out.print(i + " ");

input /= i;

}

}

System.out.println();

}

}

It compiles fine and even properly factors SOME integers but not for all. For example, when I enter 16, I get the correct answer 2 2 2 2 or if I enter 150 I get 2 3 5 5 which is correct. However when I start to use other simple numbers like 80 I get 2 2 2 2 which is incorrect or when I type 9684 i get 2 2 3 3 which is also incorrect. Is there a logic error that I am missing?

Thanks for any help in advance! =)

Re: Can not see why this Prime factorization is not working

Hello falco,

please use the [CODE]-tags when posting code to maintain the Java highlighting and the structure of your code.

Judging by your code, your for-loop is running as long as your **int i** is less than your specified integer to factorize, which obviously will give you the wrong result since you need to run the for-loop as long as int i is less or equal to your specified integer.

I guess that adding a simple " = " in your for-loop will give you the correct results.

Code:

` for (int i = 2; i <= input; i++)`

Cheers,

Z!

Re: Can not see why this Prime factorization is not working

Thank you very much, ugh such a simple mistake :(doh): Sorry my first post, I'll add the [CODE] tag next time.:)-:

Re: Can not see why this Prime factorization is not working

Glad it worked out! Good luck!