It is not the code which is the problem, but the algorithm. When factorising you should search for the smallest factors first and divide the number to factorise by that number. Then check if the result is a prime. If not, then find its smallest factor and divide the result by its smallest factor. Repeat this proces until the result of the division is a prime.

For example, take the number 50. This is not a prime number and its smallest divisor is 2. Now 50 / 2 = 25. Also 25 is not a prime number, its smallest divisor is 5. Now 25 / 5 = 5. Number 5 is a prime number, so the factorisation is: 50 = 2 x 5 x 5.

Code:

#include <stdio.h>
int main()
{
int inp, div;
printf("What number do you want the factors of?\n");
scanf("%d", &inp);
while (div != -1)
{
div = find_smallest_divisor (inp);
if (div == -1)
{
/* No smallest divisor found, so current value of
inp is a prime number and therefore it is the last
factor. */
printf ("%d\n", inp);
}
else
{
/* Smallest divisor found, so current value of inp is not
a prime number and can be divised by div. */
printf ("%d X ", div);
inp /= div;
}
}
return 0;
}
int find_smallest_divisor (int i)
{
/* Find the smallest divisor of a value i. */
int j;
int r = -1;
for (j = 2; j < i && r == -1; j++)
{
if (i % j == 0)
{
r = j;
}
}
return r;
}