Answer to Question #324271 in C++ for micheal

Question #324271

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


1
Expert's answer
2022-04-09T02:51:44-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS