Write a program that uses for loops to perform the following steps:
Output the sum of all even numbers between firstNum and secondNum
#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;
}
Comments
Leave a comment