Need Help with adding sum of multiple even numbers

So in my program i need it to add all the even numbers in between and its just adding 2 numbers together. For example if I enter 2 and 14 the total is 16. But it is suppose to be 40 because 4+6+8+10+12. Here is what I have so far

#include <iostream>
#include <string>

using namespace std;
int main()
{

// lets declare some variable first.
int positiveSum =0; //this will hold sum of positive nums
int totalSum =0; // this will hold sum of all the nums

int number=0; // user input for number
for (int i = 1; i <=2; i++) // loop from 1 to 10 times
{
cout << " Enter a number: ";
cin >> number;

// now check if number is positive or negative
if (number >=0)
{
positiveSum += number; // adds this number to positiveSum
}
else if (number < 0)
{

}

}

// So finally add the positiveSum and negativeSum to get the totalSum
totalSum = positiveSum;

cout << endl;
cout << " Total of Positive numbers is: " << positiveSum << endl;
cout << " Total of all numbers is: " << totalSum << endl;

system ("pause");
return 0;
}
closed account (z05DSL3A)
Did you post the wrong code?
ok, assuming you took a crack at it and are very new and very confused, try something like:

cin >> start;
cin >> end;
if(start%2) start++;
for(result = 0, i = start; i <= end; i+=2) result +=i;

that should serve as the core of what you need to do, you can add couts and other logic as needed.

I am pretty sure you can also just compute it directly. Someone bored may come along... but using the old 1-n sum formula should be able to craft the whole thing in one cout statement.
Last edited on
man i'm really confused jonnin. And yes im very new lol
krimsoh, to clear up your confusion, try doing the task in smaller steps.

1. Since you need to add the even numbers between a lower and upper limit, write a program that prompts for the lower and upper limits. Store the limits in two variables and then print them out.

2. Once you have that working add code (located after you print out the two limits) to add up the even numbers between the two. Print the sum.
To add to @dhayden's train of thought...

I would suggest:

step 1.1 Once you can print out the two limits, Print out all of the numbers between the 2 limits.

step 1.2 Print out just the even numbers between the 2 limits.

Then go on to step 2.

Start small. Code. Execute. Verify. Repeat.

Only add a new capability after the previous capability is shown to work.
Last edited on
Someone bored may come along...
Not today. Same task was solved some days or few weeks ago. Alas, the Search on top of the window goes through the internet instead of this list only. Hard cheese.
Last edited on
closed account (z05DSL3A)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* 
** So in my program i need it to add all the even numbers in betweenand its just 
** adding 2 numbers together.For example if I enter 2 and 14 the total is 16. But 
** it is suppose to be 40 because 4 + 6 + 8 + 10 + 12.
*/
#include <iostream>
#include <limits>

int main ()
{
    int number1{};
    int number2{};
    //
    // A method of prompting the user and validationg the input
    // don't worry to much about what it is doing yet, it will 
    // save you a headache if you enter a letter
    while ((std::cout << "Please enter the first number : ") &&
          (!(std::cin >> number1) || (number1 < 0)))
    {
        std::cout << "Error: invalid input.\n";
        std::cin.clear ();
        std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
    }
    //
    while ((std::cout << "Please enter the second number : ") &&
        (!(std::cin >> number2) || (number2 < number1)))
    {
        std::cout << "Error: invalid input.\n";
        std::cin.clear ();
        std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
    }

    int index{ number1 };
    int sum{};

    if (index % 2 == 0) 
        index += 2;
    else
        index++;

    while (index < number2)
    {
        sum += index;
        index += 2;
    }

    std::cout << "The sum of the even numbers between " << number1
        << " and " << number2 << " is " << sum << "\n\n";

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   int a, b;
   cout << "Input a and b, with a < b: ";
   cin >> a >> b;

   b -= 2 - b % 2;
   a += 2 - a % 2;
   if ( b < a )
   {
      cout << 0 << '\n';
   }
   else
   {
      int n = 1 + ( b - a ) / 2;
      cout << n * ( b + a ) / 2;
   }
}
In the age of AI I do not like computers to boss me around with tiring requirements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   int a, b, c;
   cout << "Input a and b: ";
   cin >> a >> b;
   if ( b < a ) swap( a, b );
/*
**   {
**      c = a;
**      a = b;
**      b = c;
**   }
*/
   a += 2 - a % 2;
   b -= 2 - b % 2;
   c = 1 + ( b - a ) / 2;
   cout << c * ( b + a ) / 2;
}
;)

Edit: call swap instead of "halfway circular shift exerting an auxiliary variable"
Last edited on
@MikeStgt:
std::swap( a, b ); would show the intent more explicitly than your lines 11-13.
Topic archived. No new replies allowed.