Finding divisors of every number up to 100?

Hello,
I am trying to find the divisors of the numbers 2 through 100, add them up, and print them out as follows:
2: 1 = 1
3: 1 = 1
4: 1+2 = 3
5: 1 = 1
6: 1+2+3 = 6
...
100: 1+2+4+5....

I know how to find the divisors of one number, but I am having trouble iterating through all numbers up to 100 (I haven't even attempted the adding and printing yet). Right now I am just trying to get a list of all the divisors for numbers 1 through 100. All that prints when I run the program is a 1. Here is my code:

#include <iostream>
using std::cout;
using std::endl;
int main () {
int num = 2;
int i = 1;
while (num < 100)
{
if (num % i ==0 && i < num)
{
cout << i << endl;
i++;
}
if (i >= num)
{
i == 1;
num++;
}
else
{
i++;
}
}
return 0;
}

Thanks!!
Is that how you sum up all factors for certain numbers?
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 <iostream>

using namespace std;

int main()
{
	int sum;
	bool bFirst;
	for(int i = 2; i <= 100; i++)
	{
		sum = 0;
		bFirst = false;
		std::cout << i << " : ";
		for(int j = 1; j < i; j++)
		{
			if(i % j == 0) // Is this number a factor?
			{
				sum += j;
				if(bFirst == false) bFirst = true; else std::cout << "+"; std::cout << j;
			}
		}
		std::cout << " = " << sum << std::endl;
	}
	
	return 0;
}


2 : 1 = 1
3 : 1 = 1
4 : 1+2 = 3
5 : 1 = 1
6 : 1+2+3 = 6
7 : 1 = 1
8 : 1+2+4 = 7
9 : 1+3 = 4
10 : 1+2+5 = 8
11 : 1 = 1
12 : 1+2+3+4+6 = 16
13 : 1 = 1
14 : 1+2+7 = 10
15 : 1+3+5 = 9
16 : 1+2+4+8 = 15
17 : 1 = 1
18 : 1+2+3+6+9 = 21
19 : 1 = 1
20 : 1+2+4+5+10 = 22
21 : 1+3+7 = 11
22 : 1+2+11 = 14
23 : 1 = 1
24 : 1+2+3+4+6+8+12 = 36
25 : 1+5 = 6
26 : 1+2+13 = 16
27 : 1+3+9 = 13
28 : 1+2+4+7+14 = 28
29 : 1 = 1
30 : 1+2+3+5+6+10+15 = 42
31 : 1 = 1
32 : 1+2+4+8+16 = 31
33 : 1+3+11 = 15
34 : 1+2+17 = 20
35 : 1+5+7 = 13
36 : 1+2+3+4+6+9+12+18 = 55
37 : 1 = 1
38 : 1+2+19 = 22
39 : 1+3+13 = 17
40 : 1+2+4+5+8+10+20 = 50
41 : 1 = 1
42 : 1+2+3+6+7+14+21 = 54
43 : 1 = 1
44 : 1+2+4+11+22 = 40
45 : 1+3+5+9+15 = 33
46 : 1+2+23 = 26
47 : 1 = 1
48 : 1+2+3+4+6+8+12+16+24 = 76
49 : 1+7 = 8
50 : 1+2+5+10+25 = 43
51 : 1+3+17 = 21
52 : 1+2+4+13+26 = 46
53 : 1 = 1
54 : 1+2+3+6+9+18+27 = 66
55 : 1+5+11 = 17
56 : 1+2+4+7+8+14+28 = 64
57 : 1+3+19 = 23
58 : 1+2+29 = 32
59 : 1 = 1
60 : 1+2+3+4+5+6+10+12+15+20+30 = 108
61 : 1 = 1
62 : 1+2+31 = 34
63 : 1+3+7+9+21 = 41
64 : 1+2+4+8+16+32 = 63
65 : 1+5+13 = 19
66 : 1+2+3+6+11+22+33 = 78
67 : 1 = 1
68 : 1+2+4+17+34 = 58
69 : 1+3+23 = 27
70 : 1+2+5+7+10+14+35 = 74
71 : 1 = 1
72 : 1+2+3+4+6+8+9+12+18+24+36 = 123
73 : 1 = 1
74 : 1+2+37 = 40
75 : 1+3+5+15+25 = 49
76 : 1+2+4+19+38 = 64
77 : 1+7+11 = 19
78 : 1+2+3+6+13+26+39 = 90
79 : 1 = 1
80 : 1+2+4+5+8+10+16+20+40 = 106
81 : 1+3+9+27 = 40
82 : 1+2+41 = 44
83 : 1 = 1
84 : 1+2+3+4+6+7+12+14+21+28+42 = 140
85 : 1+5+17 = 23
86 : 1+2+43 = 46
87 : 1+3+29 = 33
88 : 1+2+4+8+11+22+44 = 92
89 : 1 = 1
90 : 1+2+3+5+6+9+10+15+18+30+45 = 144
91 : 1+7+13 = 21
92 : 1+2+4+23+46 = 76
93 : 1+3+31 = 35
94 : 1+2+47 = 50
95 : 1+5+19 = 25
96 : 1+2+3+4+6+8+12+16+24+32+48 = 156
97 : 1 = 1
98 : 1+2+7+14+49 = 73
99 : 1+3+9+11+33 = 57
100 : 1+2+4+5+10+20+25+50 = 117
Wouldn't the divisor also be the number itself? If the number is 50, the divisor would be 1, 2, 5, 10, 25 and also 50
Last edited on
Hengry, sorry I forgot to mention I'm doing all divisors excluding the number itself; otherwise you'd be right.

Thank you Mantor22!
Part of the issue was an == where there should have just been =. I used a sum variable but did it differently, since I don't really understand how to use bool. However, I'm stuck at how to not end each line of divisors with an extra '+'. Here is my code:
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
#include <iostream>
using std::cout;
using std::endl;
int main () {
        int num = 2;
        int i = 1;
        int sum = 0;
cout << num << ":";
while( num < 20 )
{       if (num % i == 0 && i < num)
        {sum = sum + i;
        cout << i << "+";
        i++;}
        else if (num % i != 0 && i < num)
       {i++;}
        else 
        {i = 1;
        num++;
        cout <<"=" << sum;
        cout << "\n" << num << ":";
        sum = 0;}
}
return 0;
}


Which gave me this result:
2:1+=1
3:1+=1
4:1+2+=3
5:1+=1
6:1+2+3+=6
7:1+=1
8:1+2+4+=7
9:1+3+=4
10:1+2+5+=8
11:1+=1
12:1+2+3+4+6+=16
13:1+=1
14:1+2+7+=10
15:1+3+5+=9
...
Topic archived. No new replies allowed.