Sum of all even numbers?

Hey I have been in C+ for two weeks now and my teacher asked this question

Write a program that calculates and prints the sum of all even numbers between 10 and 40?

Well after alot of time I am stuck, I know my code right...at least I think but it doesn't show anything on the screen. The command prompt pops up a split second then disappers....so it has to be something really simple right?

Here's the code

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

int main()
{
int x;
int i;
i=0;
x=10;
while (x <= 40)
{
i=i+x;
x=x+2;
}
printf("Sum of all even numbers between 10 and 40(inclusive) is %d\n",i);
return 0;
}
BTW, a for loop would be a better choice for what you a trying to do:

1
2
3
4
5
for(int x = 10; x <= 40; x+=2) { //int x = 10 is defining the variable
//x <= 40 is the condition under which the loop runs
//and x+=2 is run after every loop "cycle"
   i+=x; //i += x is the same as i = i + x
}
Why not just make it the shortest possible?
for (int x=10;x<=40;i+=x,x+=2);
We'll I'd tell my teacher that this can be done at compile time and the fastest known algorithm is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

template< size_t N >
struct sum {
  enum { value = ( N + 1 ) * N / 2 };
};

template< size_t N >
struct sum_even {
  enum { value = 2 * sum<N/2>::value };
};

int main() {
  std::cout << "The answer is " <<
    sum_even<40>::value - sum_even<8>::value << std::endl;
}


since this code compiles to
 
std::cout << "The answer is " << 400 << std::endl;


Hey guys so far this site has taught me alot...better than this book they gave. But I looked at the forum that helios posted and some of it make sense and some doesn't apparently their is tons of ways to pause the screen to see your output. But could someone show me the basic way of doing this with the isostream, with my code included? Please?

@jsmith
I test your code, later, thank you very much



AND to the rest thank you so much for helping me. I will probabley be on this site alot now. This is a homework assignment, but you can see I have tried. And my teacher doesn't care about forum help as long as I get it.
the fastest known algorithm is

Thats BS. You could as well compute the result by hand, insert the number and claim it's fast. Just that you use the C++ compiler to calculate it doesn't make your algorithm fast (on the contrary, one could argue that you make C++ into a interpreted language, and considering my compile times, a fairly slow one). Furthermore it is "neat", and "neat" solutions should be avoided. (Yes, there are cases where TMP is a Good Thing. But this isn't one of them)

Use the formula from jsmith, but don't use the nifty template stuff. It's kinda cool, but you should stay away from it until you have a very good understanding of the language (here, templates are used in a completely different way than they were intendet to.)
kiloraw, the simplest way of pausing a console window is using 'system("PAUSE")'
exception:

In terms of execution time of the program, it is the fastest since it simply outputs 400 without
doing any calculations. In terms of compilation time it is probably slightly slower.
Compilation speed is a hardware problem. Upgrade your computer.

Absolutely disagree on your argument re: neat solutions. If execution time is of concern then
doing the calculation at compile time rather than execution time is the best possible solution.

I posted it knowing full well that the original poster couldn't use my code for his/her homework
since it used concepts well beyond his/her class. My point was simply to plant a seed that
there are these things called "templates" in the language and to show that there is more than one way to solve the problem.
Why don't you just find the pattern, which is that the sum from 0 to x, given x is even, of all in-between even numbers, inclusive, is:

1
2
3
4
// Assuming x is an integer, thusly integer division, resulting in floor

(x%4 == 0)? x * x/4 + x/2 : x * (x/4 + 1);


and then if you want to do from 10 to 40, since you're example shows you won't include 10, you insert 40 for x and subtract the sum when x is 10.

1
2
3
4
5

#define sum(x) (x%4 == 0)? x * x/4 + x/2 : x * (x/4 + 1)

int answer = sum(40) - sum(10);


edit: this is because:
0 + x = x
2 + x - 2 = x
4 + x - 4 = x etc.

When you reach x/2, if x/2 is even, it doesn't equal x, it's x/2 (easy enough). x/2 is even if x is divisible by 4.

I don't understand how this went through 8 posts of looping, really.
Last edited on
I think that is the most convoluted solution suggested yet. No looping is even needed to solve the problem. An algorithm that runs in constant time for all inputs is always better than one that runs in linear time for all inputs.
You must not have tried my code. Or don't know what convaluted means.

Edit: Yeah, sorry, at first I thought you were insulting my code. Did you mean concise?
Last edited on
Topic archived. No new replies allowed.