# Thread: Find all factors of a number

1. ## Find all factors of a number

I have to create a function that finds all factors of any particular number. This is what i've got so far, but it doesn't seem to work all the time. It only really divides the number you input by two, which only gives one factor of even numbers. Therefore, I'm almost positive that my problem exists in the () part of the for() loop.

Code:
```#include <stdio.h>
#include <math.h>

main()
{
int i, a;

printf("Input a number, I'll tell you its factors.\n");
scanf("%d", &a);

for(i = 2; i <= a; ++i)
{
if(a % i == 0)
{
printf("%d * ", a / i);
}
else
break;
}
}```
Any help would be greatly appreciated.

2. double post, sorry.

3. the a % i part is correct, but why do print a/ i? because a %i == 0 this means that i is a factor of a, so you need to print i.
And why break when (a % i != 0) ? You should continue the loop to check all the numbers that are smaller than a.
One final note, you don't need <math.h> , this header file is for functions like sqrt and sin.
HTH

4. Or half the numbers smaller than 'a' rounded up to the nearest whole?

5. Originally posted by Azuth
Or half the numbers smaller than 'a' rounded up to the nearest whole?
I was going to suggest this when he/she gets the program working well.
Do not forget that 1 and a are factors of a too, so you need to print them when the loop starts/ends.

6. Simply remove the
Code:
```else
break;```
and start the for() loop at 1 to get glUser3f's suggestion about 1 and a

Popular pages Recent additions