-14

I'm new in C++ and I have been thinking about this.

There is a question that asks:

Find the sum of the multiples of 2 under 547.

How am I supposed to code the program to keep on multiplying the user input and adding them at the same time until they reach the number under 547, then stop the process and count the result?

halfer
  • 19,824
  • 17
  • 99
  • 186
Vancer
  • 55
  • 3
  • 10
  • 4
    I'm afraid you're going to get down-voted into the abyss if you don't at least show an attempt at solving the problem and explain where you get stuck. – jas Aug 20 '15 at 16:03
  • 2
    Please use your favorite math text book from middle school to find the meaning of the mysterious "multiples of 2" (hint: it's a "geekspeak" expression for something familiar to every first grader). Once you figure this out, you would understand that (1) there's no user input, and (2) there's no multiplication. – Sergey Kalinichenko Aug 20 '15 at 16:03
  • https://en.wikipedia.org/wiki/Arithmetic_progression#Sum – ex-bart Aug 20 '15 at 16:04
  • 1
    @dasblinkenlight On the contrary, the most efficient way to solve this **is** by multiplication :P ... `2 + 4 + 6 + ... + 546` = `2 * (1 + 2 + 3 + ... + 273)` = `2 * (273 * 274 / 2)` = `74802` -- See: https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF – Tom Lord Apr 26 '18 at 16:40
  • However, for the purpose of this question I suspect that's "cheating" :) – Tom Lord Apr 26 '18 at 16:41

2 Answers2

4

Find the sum of the multiples of 2 under 547

Lets break this down...

A "Multiple of two" is a number that can be divided by two evenly. Meaning there is no remainder. Look at this link for more info.

Ok we know what a "Multiple of two" is now lets look at the sum...

We want to add up "Multiples of two" under 547. Meaning we will count up to 547 by two and add them together.

so 2 + 4 + 6 + .... + 544 + 546 = A big number

Like @Mureinik posted...we start by defining and initializing something to hold our sum (adding up all the counting by two) in.

long sum = 0;

Then we need to count up to 547 by twos.

for(int i = 0; i < 547; i += 2){

} 

This is a for loop. It will execute what is inside the { } until the condition is false. It defines i as the integer of 0. The condition is i < 547. It will also increment i by two each time through the loop. That is what the i += 2 part does. (i += 2 is the same thing as i = i + 2)

So now we have something to hold our summation in (sum) and we have a means of counting by two (the for loop). All we have to do is add up the numbers.

sum += i;

This will take care of that for you. Like you hopefully have guessed sum += i is short hand for sum = sum + i;. i will always be a "Multiple of two" because we are always adding 2 to i. Once i gets to 548 it will fall out of the loop and stop adding up the sum.

Like others have commented it will not hurt to look up some tutorials on the web. I know that it can be over whelming you just have to stick with it.

Hope this clears up some stuff for you. Code On.

Noah Herron
  • 630
  • 4
  • 23
3

You just need to iterate over the multiples and sum them:

long sum = 0;
for (int i = 0; i < 547; i +=2) {
    sum += i;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • May I ask why you are using a `long` for `sum` when you know that 547 is less than `MAX_INT` – Noah Herron Aug 20 '15 at 16:09
  • 5
    547 is indeed much less than `MAX_INT`, but the sum of all the items is `74256`, which may be larger than `MAX_INT`, depending on the platform. – Mureinik Aug 20 '15 at 16:11
  • 1
    Didn't quite get you, can we skype? – Vancer Aug 20 '15 at 16:12
  • Thanks. Had a brain fart. You are right. I was not thinking logically. – Noah Herron Aug 20 '15 at 16:12
  • I got it too thank you but, Noah, can you help me a bit – Vancer Aug 20 '15 at 16:18
  • 4
    @Vancer Take dasblinkenlight's advice from above and [crack some books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and [read some tutorials](http://www.cplusplus.com/doc/tutorial/control/) for support. New to programming is fine. However being unable to interpret a for loop demonstrates so little knowledge that it will be next to impossible to explain anything. That or trolling. – user4581301 Aug 20 '15 at 17:38
  • What do you need help with? – Noah Herron Aug 20 '15 at 19:14