Answer to Question #172128 in C++ for sara

Question #172128

Write a program that uses for loops to perform the following steps:

 Output the sum of all even numbers between firstNum and secondNum


1
Expert's answer
2021-03-19T12:36:08-0400
#include <iostream>


using namespace std;


int main()
{
    int firstNum, secondNum;
    cout << "Enter first number: ";
    cin >> firstNum;


    cout << "Enter second number: ";
    cin >> secondNum;


    int sumOfAllEvens = 0;
    for(int i = firstNum; i <= secondNum; i++)
    {
        if(i%2==0)
            sumOfAllEvens += i;
    }
    cout << "Sum of all even numbers: " << sumOfAllEvens << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS