so, we have this school assignement in which we are asked to make a program that would ask you to enter two integers, them the program should add the two integers and all the even integers between them.

example:

integer1: 1
integer2: 10

(1 + 10 + 2 + 4 + 6 + 8) = 31

it is killing me. i understand how "for" is used. but i could not figure out how to construct the solution. i tried including modulo(%) to the solution but really can't program it right. healp me, please. :(

Recommended Answers

All 5 Replies

Hint: assuming that your iterator variable is i, try using i += 2 instead of i++. In other words,

for (i = 2; i < final_value; i += 2)

This will step you two values at a time istead of just one.

Are you allowed to use vectors? If so, this is a way to think of it. In no means, is this the final solution. Just a way for you to think about it. I would use a vector to store the values in, then, create another vector to store the even numbers in and finally calculating the sum of all the values inside the Even vector. Here's an example:

#include <iostream>     // std::cout
#include <algorithm>    // std::count_if
#include <vector>       // std::vector
#include <numeric>
using namespace std;

bool IsOdd (int i) { return ((i%2)==1); }

int main()
{
    int number1;
    int number2;

    int summation = 0;
    std::vector<int> values(number2); 
    std::vector<int> evenValues;

    cout << "Please enter your first number: ";
    cin >> number1;

    cout << "Please enter your second number: ";
    cin >> number2;

    for(unsigned i=number1; (i < number2 + 1); i++)
    {
        values[i] = i;

    }
    // Count all the variables that are even

    for(unsigned j=0; (j < number2+1); j++)
    {
        if(!IsOdd(values[j]))
        {
            evenValues.push_back(values[j]);
        }
    }

    summation = std::accumulate(evenValues.begin(), evenValues.end(), 0);

    cout << summation << endl;


}

Another hint that might help you is this. The first even number higher than x can be calculated as 2 * ((x + 2) / 2) (and the parenthesis matter, because this relies on integer division).

You can use Schol-R-LEA's loop to do the rest.

In plain simple terms
1 Just take in two numbers
2 Starting from the smallest number input to the largest
3 At the end of the loop print the results
and we are done

The code which I have provided compiles and runs well on
Ubuntu linux and Code Blocks Ide

#include <iostream>
using namespace std;
int main()
{
    int firstnum, secondnum, result;
    firstnum = secondnum = result = 0;

    cout<<"Input a number ";
    cin>>firstnum;

    cout<<"Input a number ";
    cin>>secondnum;

    if(firstnum == secondnum){ cout << "Nothing to do"; return 0;}

    if(firstnum < secondnum)
    {
        for(int i=(firstnum+1);i<secondnum;i++)
        {
            cout<<"i ="<<i<<endl;
            if(i%2==0)
            {
                   result = result + i;
            }
        }
     }
     else
     {
        for(int i=(secondnum+1);i<firstnum;i++)
        {
            cout<<"i ="<<i<<endl;
            if(i%2==0)
            {
                   result = result + i;
            }
        }
     }
     cout << "The result is = :"<<result<<endl;

return 0;
}

Simple solution: use the mod % operator to determine if a number is odd or even, then if even just add to the sum. Note: the only reason I'm posting entire code is because this thread is already over 2 weeks old, so it probably won't affect the OPs program one way or the other.

int n1 = 1;
int n2 = 10;
int sum = n1 + n2;
for(int number = n1; number <= n2; number++)
{
   if( (number % 2) == 0) 
   {
      sum += number;
   }
}

No need for all that complicated math or use of vectors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.