0
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

int main()
{
    // Declarations
    int firstNum = 0, secondNum = 0;

    cout << "This program will ask you for two numbers, and then output the even numbers between those two numbers" << endl;
    cout << "Please enter the lowest number: " << endl;
    cin >> firstNum;
    cout << "Please enter the highest number: " << endl;
    cin >> secondNum;

    if (firstNum % 2 != 0 && secondNum % 2 != 0)
    {
        while (firstNum <= secondNum)
        {
            if (firstNum % 2 != 0)
            {
                cout << firstNum << " ";
            }

            firstNum++;
        }
    }

    else if (firstNum % 2 == 0 && secondNum % 2 == 0)
    {
        while (firstNum <= secondNum)
        {
            if (firstNum % 2 == 0)
            {
                cout << firstNum << " ";
            }

            firstNum++;
        }
    }

    return 0;
}

I'm doing a coding exercise for c++, if the user inputs two odd numbers the program would then output all of the odd numbers between the first and second number (first number has to be smaller than the second). If both numbers are even then the program will out put the sum of the even numbers between the first and second (again the first has to be the smallest). My problem is that I do not know how to add the numbers together, I only know how to print either the even or odd numbers between them.

An example:

  • first number = 4
  • second number = 36
  • output = 340

how do you add the even numbers between them?

2
  • also ignore the first cout message I forgot to get rid of that, it's not really needed.
    – Darunia
    May 10, 2020 at 1:21
  • Use a int sum = 0;, and instead of the cout do sum+=firstNum;
    – cigien
    May 10, 2020 at 1:22

2 Answers 2

0

You could have a variable that stores the result. For example

else if (firstNum % 2 == 0 && secondNum % 2 == 0)
{
    int sum = 0; // store the sum of all even numbers
    while (firstNum <= secondNum)
    {
        sum += firstNum; // adding the number to the sum
        firstNum += 2; // increment by 2 would skip all the odd numbers
    }

    cout << sum << endl;
}

Hopefully this helps!

1
  • My code still does not work, it gives me a really big number. My code is in the link below.
    – Darunia
    May 10, 2020 at 1:53
0

My problem is that I do not know how to add the numbers together - If that is the exact question and assuming start and end are the start and end numbers you can do this to get the sum of all numbers between start and end

int sum = 0;
for ( int i = start; i <= end; i++  )
{
       sum += i;
}   
std::cout << "Sum of numbers between " << start << " and end " << end << " is " << sum ;

to get the sum of all even numbers in that range

int sum = 0;
for ( int i = start; i <= end; i++  )
{
    if ( i%2 == 0 ) // even
        sum += i;
}   
std::cout << "Sum of even numbers between " << start << " and end " << end << " is " << sum ;

I see you wanted it using while loop in which case

int sum = 0;
while ( start <= end  )
{
    sum += start;
    ++start;
}
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.