Answer to Question #275113 in C++ for Hena

Question #275113

1. Write a c++ program that sum's up the even numbers between 0 and



any positive integer number n given by the user.

1
Expert's answer
2021-12-03T12:32:50-0500
 #include <iostream>
using namespace std;

int main() {
    int n, sum;


    cout << "Enter a positive number: ";
    cin >> n;
    sum = 0;
    for (int i=0; i<=n; i++) {
        if (i%2 == 0) {
            sum += i;
        }
    }
    cout << "The sum of all even number between 0 and " << n 
         << " inclusive is " << sum << endl;
}

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