Answer to Question #107663 in C++ for Abenezer

Question #107663
Add the even numbers between 0 and any positive integer number given by the user
1
Expert's answer
2020-04-03T15:07:33-0400
#include <iostream>

using namespace std;

int main() {

    int i, n, sum = 0;

    // Take input from user.
    cout << "Print sum of even numbers till : ";
    cin >> n;

    for(i = 0; i <= n; i++) {

 // Check for even or not.
        if((i % 2) == 0) {

            sum += i;

        }
    }

    cout << endl << "Sum of even numbers from 0 to " << n << " is : " << sum;

    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