# Thread: C++ beginner - Sum of multiples less than 100

1. ## C++ beginner - Sum of multiples less than 100

Hey guys, this is my first post here. I am a beginner in C++ and have a question for a homework assignment. I have read my book over and over and can't seem to figure out how to code this properly.

What I want it to do is have the user input a number: ex. 5 and then have the code give the sum for all the multiples of 5 less than 100, so it should be equal to 950 in this example.

Anyway, I have got so far to get it to list the numbers, but I need the sum of those numbers without all the text displayed every time.

I have chosen to use the FOR, but I am wondering if I should implement WHILE loop in my coding somewhere. I just don't know how to go about it for this problem.

Again, this is my first post (and definitely not my last!) I hope I have read the rules properly. It is my first programming class in school, and I am seeing a tutor tomorrow and the next day, I would just like to have a better grasp of this before going in tomorrow.

Code:
```#include <iostream>
using namespace std;

{
// Start
cout << "What multiples are we adding? ";
int start;
cin >> start;

int increment = start;

for (int count = start; count <= 100; count += increment)
cout << "The sum of multiples of " << start
<< " less than 100 are: " << count << endl;

return 0;
}

int main()
{
return 0;
}```
I want the output like this:

What multiples are we adding? 5
The sum of the multiples of 5 less than 100 are: 950

Thanks for your help and explanations!

2. A for loop is fine. At the moment, the problem is that you are not adding up the multiples of 5 (or whatever it is).

I suggest that you change your addMultiples function to take start as an argument, and to return the result. The input/output should all be done in the main function instead.

3. Originally Posted by laserlight
A for loop is fine. At the moment, the problem is that you are not adding up the multiples of 5 (or whatever it is).

I suggest that you change your addMultiples function to take start as an argument, and to return the result. The input/output should all be done in the main function instead.
Thanks I will try to change the function to getMultiple, then have a function run the FOR loop. Any advice on how to get the sum from a loop?

Thanks again, I feel I am on the right track!

4. Originally Posted by Bronzy
Any advice on how to get the sum from a loop?
You need another variable, such as "sum", that is initialized to zero, and you need to add count to that sum for each iteration of the loop.

5. Originally Posted by Bronzy
Any advice on how to get the sum from a loop?
Tried making a flowchart?

6. Thanks for the help guys, I was able to have main call a few separate functions, I was missing the curly braces for the loop to get the Sum.

Cheers!

Code:
```#include <iostream>
using namespace std;

/**********************************************************************
* getMultiple prompts the user for the multiple they want.
************************************************************************/
int getMultiple()
{
cout << "What multiples are we adding? ";
int number;
cin >> number;
return number;
}

/**********************************************************************
* addMultiples initiates a loop for the multiple and sums the data.
************************************************************************/
{
int sum = 0;
int end = 100;

for (int count = multiple; count < 100; count += multiple)
{
sum += count;
}

return sum;
}

/**********************************************************************
* Display outputs the sum of the multiples to the user.
************************************************************************/
void display(int multiple, int sum)
{
cout << "The sum of multiples of "
<< multiple
<< " less than 100 are: "
<< sum
<< endl;

return;
}

/**********************************************************************
* Main calls the other functions in the program
************************************************************************/
int main()
{
int multiple = getMultiple();

display(multiple, sum);

return 0;
}```

7. Looks good. I note that you forgot to use the end variable in addMultiples though.

8. Originally Posted by laserlight
Looks good. I note that you forgot to use the end variable in addMultiples though.
Aha, you are right! I'll change that too