FOR LOOP Help! Sum of the Multiples

Trying to make a formula to show the Sum of the Multiples of a given number less than 100. Here is my code I cannot figure it out and get it to work right! Please Help

1
2
3
4
5
6
7
8
9
10
 cout << "What multiples are we adding? ";
   int multiple;
   cin >> multiple;
   for (int sum = multiple; sum < 100; sum + sum)
      cout << "The sum of multiples of "
           << multiple
           << "less than 100 are: "
           << sum
           << endl;
shouldn't line 6 be << sum - sum? since you are only adding the two "sums" together and not getting a new value for multiple. Also try sum += sum in the for loop.
Last edited on
From just looking at your code your not incrementing anything so sum never changes; ( sum += sum ) after the for loop than try to cout and if that does not work try a function and return a value for each time through the loop.
I have tried sum += sum and it gives me 3 lines going up to 100. but I am looking for the sum of those multiples. which lets say for 5 should be 950. but when I do 5 it goes 5 10 20 40 80 thats it when I want it to show the SUM of all the multiples.
are you talking about 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17....? Because currently you are just doubling the last value..
Last edited on
I need it to do this
lets say I enter the value of 5
I need it to show the sum of the multiples of 5 being
5+10+15+20+25+30+35+40+45+50+55+60+65+70+75+80+85+90+95=950 and I need it to show that answer.
Maybe try something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <limits>
int main()
{
     auto input = 0 , total = 0;
     do
     {
        std::cout << "Please enter a value between 1 and 100\n> " << std::flush;
        std::cin >> input;
        std::cin.clear();
        std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' );
     } while( input == 0 || input > 100 );

     for( auto i = 0 ; i < 100; i += input)
     {
         total += i;
     }

     std::cout << total << std::endl;
}
Please enter a value between 1 and 100
> 5
950

Process returned 0 (0x0)   execution time : 1.283 s
Press any key to continue.


See the for loop? I am incrementing i by the inputed value then adding that to the total. I got a result of 950 when I added it up.
Last edited on
cout << "What multiples are we adding? ";
int multiple;
int I;
int total;
cin >> multiple;
while ( multiple == 0 || multiple > 100);
for (int I = 0; multiple < 100; I += multiple)
cout << "The sum of multiples of "
<< multiple
<< " less than 100 are: "
<< I
<< endl;
{
total += I;
}
return 0;
I am trying to use what you are showing me when I try to run the program it gives me an infinite loop and the program wont close it keeps counting in increments of whatever the input is forever.
first of all you are forgetting the do all you have is a while statement.
do/while loops run a minimum of 1 time. while loops run a minimum of 0 times. Also you are not initializing a value of total do total = 0 when you declare it so it is initialzed with a value of 0. Otherwise when you cal total += i it is going to be an undefined value. Also you should try to indent like I have done. after each curly brace you move to the right 4 spaces or a horizontal tab. Also it should be i < 100 because multiple never changes. The I is what is changing the I becomes the 5 , 10 , 15 ect..because you are adding the value entered to i each time. Think about it.
for( int i = 0; i < 100; i += multiple) says this multiple == 5.
i == 0;
i == 5;
i == 10;
i == 15;
i == 20;
i == 25;
i == 30;
.....
i == 100;
i cannot be greater than 100 so the loop ends.
Last edited on
Ok I am trying to do those things and it is not working for me. Thank you tho! I will have to go in a ask someone in my class
Show me what you have? I told you what lines have the errors.
Line 4 , 5 , ( 0 missing do before first cout so I call it position 0) , line 6 , line 7-10.
Last edited on
int main()
{
cout << "What multiples are we adding? ";
int multiple;
int I;
int total;
cin >> multiple;
do
{
total = 0;
}
while ( multiple == 0 || multiple > 100);
for (int I = 0; I < 100; I += multiple)
cout << "The sum of multiples of "
<< multiple
<< " less than 100 are: "
<< I
<< endl;
{
total += I;
}
return 0;

}
Now it counts all the way to 95 then stops rather than showing the sum. Thank you for your willingness to help.
Wrong. It is counting to 100. The problem is you are not outputting the last one. As you can see in the for loop you out then add to the value of total.
A Quick fix would be to output the total after the for loop ends like I did.
1
2
3
4
5
for(....)
{
...
}
std::cout << total << std::endl;

Edit let me fix your code up a lil with notes...

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
27
28
29
30
int main()
{
//cout << "What multiples are we adding? ";
int multiple;
int I;
/*int total;*/ int total = 0;
//cin >> multiple;
do
{
//total = 0;
std::cout << "What multiples are we adding( 1 - 100 );
std::cin >> multiple; 
}
while ( multiple < 1 || multiple > 100); //If you do not clear and ignore you 
//will have a major problem if someone inputs a letter.
for (int I = 0; I < 100; I += multiple){
/*cout << "The sum of multiples of "
<< multiple
<< " less than 100 are: "
<< I
<< endl;*/
total += I;
std::cout << "The sum of multiples of " << multiple << " less than 100 are "
<< I << " Which has a total so far of " << total << std::endl;
//{
//total += I;
}
return 0;

} 


Also may I suggest you learn proper indenting.
Last edited on
ok so i changed it to output the totals and it gives me
What multiples are we adding? 5
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
The sum of multiples of 5 less than 100 are: 0
I just updated my last post check it out.
Ok I will type this out without comments and hopeful it will work. I appreciate your help.
int main()
{
int multiple;

int total = 0;
cin >> multiple;
do
{
cout << "What multiples are we adding? ";
cin >> multiple;
}
while (multiple < 1 || multiple > 100);
for (int I = 0; I < 100; I += multiple)
{
total += I;
cout << "The sum of of multiples of " << multiple << " less than 100 are " << total << endl;
}
return 0;
}
I am now getting to where it is showing me this response.
What multiples are we adding? 5
The sum of of multiples of 5 less than 100 are 0
The sum of of multiples of 5 less than 100 are 5
The sum of of multiples of 5 less than 100 are 15
The sum of of multiples of 5 less than 100 are 30
The sum of of multiples of 5 less than 100 are 50
The sum of of multiples of 5 less than 100 are 75
The sum of of multiples of 5 less than 100 are 105
The sum of of multiples of 5 less than 100 are 140
The sum of of multiples of 5 less than 100 are 180
The sum of of multiples of 5 less than 100 are 225
The sum of of multiples of 5 less than 100 are 275
The sum of of multiples of 5 less than 100 are 330
The sum of of multiples of 5 less than 100 are 390
The sum of of multiples of 5 less than 100 are 455
The sum of of multiples of 5 less than 100 are 525
The sum of of multiples of 5 less than 100 are 600
The sum of of multiples of 5 less than 100 are 680
The sum of of multiples of 5 less than 100 are 765
The sum of of multiples of 5 less than 100 are 855
The sum of of multiples of 5 less than 100 are 950
where as I only need it to show.
The sum of multiples of 5 less than 100 are 950
then put line 16 outside of the for loop eg line 17 after you move the brace to line 16.
That was it! thank you!
For extra fun, think about http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

The multiple is M. The sum equals 1*M + 2*M + .. + n*M == (1+2+..+n)*M.
n*M < 100, so 100/M should get you close to n.
Topic archived. No new replies allowed.