Question #324271

Write a c++ program to find sum of all even numbers between 1-n.- using for loop


Expert's answer

#include <iostream>
using namespace std;


int main() {
    int n;


    cout << "Enter an integer n: ";
    cin >> n;


    int sum = 0;
    for (int i=1; i<=n; i++) {
        if (i%2 == 0) {
            sum += i;
        }
    }
    cout << "The sum of all even numbers betwen 1 and " << n
         << " is " << sum << 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!

LATEST TUTORIALS
APPROVED BY CLIENTS