Finding Factor of a Range of Number

I am finding it difficult to output the factors of a number from a particular range like (20 -30). Here is my code :


#include <iostream>
using namespace std;
int input,fun_count,sum;
int main()
{

cin>>input;
for(int k=input; k<=30; k++){
for(int i=1; i<=k; i++)
if(k%i==0){
fun_count =0;
fun_count++;
sum += fun_count;
cout<<k<<" "<<"the total factor for this number is equal to "<<" "<<sum<<" "<<endl;
}
else
break;
}
system("Pause");
}






here is my output:
20
20 the total factor for this number is equal to 1
20 the total factor for this number is equal to 2
21 the total factor for this number is equal to 3
22 the total factor for this number is equal to 4
22 the total factor for this number is equal to 5
23 the total factor for this number is equal to 6
24 the total factor for this number is equal to 7
24 the total factor for this number is equal to 8
24 the total factor for this number is equal to 9
24 the total factor for this number is equal to 10
25 the total factor for this number is equal to 11
26 the total factor for this number is equal to 12
26 the total factor for this number is equal to 13
27 the total factor for this number is equal to 14
28 the total factor for this number is equal to 15
28 the total factor for this number is equal to 16
29 the total factor for this number is equal to 17
30 the total factor for this number is equal to 18
30 the total factor for this number is equal to 19
30 the total factor for this number is equal to 20
Press any key to continue . . .
Last edited on
Note that you reset the value of fun_count to zero for every time (k%i == 0). And then you increment fun_count by one. So therefore the value of fun_count is always 1 when you add it to sum.

Cheers,
Thank i really appreciate your response. It is working well.

If i want to output the number with the maximum factors how do i go about getting this different factors and selecting the number with the highest number of factors.
So you want to count the number of factors that a number has?
Topic archived. No new replies allowed.