for loop to sum all even ints

I was assigned to write a program that could sum all even numbers between two numbers that a user inputted (assuming the first number was less than the second number). We have to use a for loop for this problem, and I cannot get the correct output/sum. Here is my code, please feel free to comment any solutions. When I input 4 and 36 as my two ints, the sum should be 340, but I keep getting 320.

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
#include <iostream>

using namespace std;

int main() {

    int firstNum;
    int secondNum;
    int loopNum;
    int sum;
    
    sum = 0;

  if (firstNum % 2 == 0)
        {
            firstNum = firstNum + 1;
        }
    
    for (loopNum = firstNum; loopNum <= secondNum; loopNum +=2)
    {
        if (loopNum % 2 != 0)
        {
           sum += loopNum;
        }
        
    }
     cout << endl <<"The sum of the even numbers is ==> " << sum << endl;
    
    
    
    return 0;
}


Last edited on
PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: You can edit your post and add the tags.

1
2
3
4
if (firstNum % 2 == 0)
{
   firstNum = firstNum + 1;
}


makes an even number into an odd number.

The logic in your for loop is to sum the odd numbers.

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
#include <iostream>

int main()
{
   std::cout << "Enter your first number: ";
   int firstNum { };
   std::cin >> firstNum;

   std::cout << "Enter your second number: ";
   int secondNum { };
   std::cin >> secondNum;
   std::cout << '\n';

   int sum { };

   for (int loopNum { firstNum }; loopNum <= secondNum; loopNum++)
   {
      if (loopNum % 2 == 0)
      {
         sum += loopNum;
      }
   }

   std::cout << "The sum of the even numbers is: " << sum << '\n';
}

Enter your first number: 1
Enter your second number: 11

The sum of the even numbers is: 30
It's more efficient to make the firstNum even and add by twos like the original program attempted to do. Here is a modified version of Furry Guy's program that does that by fixing the original bug:
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
#include <iostream>

int main()
{
   std::cout << "Enter your first number: ";
   int firstNum { };
   std::cin >> firstNum;

   if (firstNum % 2 == 1) {	// if firstNum is odd
       ++firstNum;		// add one to make it even
   }

   std::cout << "Enter your second number: ";
   int secondNum { };
   std::cin >> secondNum;
   std::cout << '\n';

   
   int sum { };

   for (int loopNum { firstNum }; loopNum <= secondNum; loopNum += 2)
   {
       sum += loopNum;
   }

   std::cout << "The sum of the even numbers is: " << sum << '\n';
}


The purpose of the assignment is to use loops, but if you were solving this problem in the real world, you'd use the formula for an arithmetic series:
1
2
3
4
5
6
7
   if (secondNum % 2 == 1) {
       --secondNum; // make secondNum even
   }

   int numTerms = (secondNum-firstNum) / 2 + 1;
   std::cout << "Doing it the easy way, the sum is "
	     << (firstNum+secondNum)/2 * numTerms << std::endl;

if (secondNum % 2 == 1) {
--secondNum; // make secondNum even
}


which is
secondNum &= 0x ... FFFFFE; //however many Fs your int supports
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
   int a, b;
   cout << "Input a, b (with 0 <= a <= b): ";   cin >> a >> b;
   a++;   a /= 2;   b /= 2;
   cout << "The sum of the even numbers is " << b * b - a * a + a + b << '\n';
}

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

int main() {
    int x, y, sum = 0;
    cout<<"Enter first number: ";
    cin>>x;
    cout<<"Enter second number: ";
    cin>>y;
    x = x + x%2;
    for (int i = x; i <= y; i +=2)
    {
      sum = sum + i;
    }
    cout <<"The sum of all even numbers between "<<x<<" and "<<y<<" is "<<sum;
    return 0;
}


To learn more about C++ for loop, please visit http://www.cplusplus.com/doc/tutorial/control/ or
https://www.alphacodingskills.com/cpp/cpp-for-loop.php
Last edited on
Topic archived. No new replies allowed.