Factors of an integer

I want to make a simple program that will print out the factors of an integer. My program right now only outputs "The prime factors of 221 are 221, 221, 221, 221"... Where it should be "The prime factors of 221 are 1, 13, 17, 221" Any help would be awesome!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdio>

int factorsOf(int x);

//function
int main()
{
   int x = 221;
   printf("The factors of 221 are ");
   factorsOf(x);
   
   return 0;
}

//function
int factorsOf(int x)
{    
   for(int i = 1; i <= 221; i++)
   {
      if (x%i == 0)
      {
         printf("%d, ", x);
      }
   }
   return x;
}
You might try printing out the factor i rather than the number x.

Giving your variables more descriptive names might make the mistake more obvious.
Ah that was definitely it. Thanks!
1 and 221 are not prime factors
Topic archived. No new replies allowed.