Answer to Question #323324 in C++ for Christine Traya

Question #323324

write a program that calculates and prints the sum of the even integers from 2 to user defined limit and product of odd integers from 1 to user defined limit. use a do-while loop that ask the user whether the program should be terminated or not also maintain a count as to how many times the user ran to do-while loop

1
Expert's answer
2022-04-04T07:47:54-0400
#include <iostream>
using namespace std;


int main() {
    long sum, prod;
    int limit;
    int count = 0;
    char ans;


    do {
        cout << "Enter a limit: ";
        cin >> limit;
        sum = 0;
        prod = 1;
        for (int i=1; i<=limit; i++) {
            if (i%2 == 0) {
                sum += i;
            }
            else {
                prod *= i;
            }
        }
        cout << "The sum of even numbers is " << sum << endl;
        cout << "The product of odd numbers is " << prod << endl;
        cout << endl << "To continye type y >> ";
        cin >> ans;


        count++;


    } while (ans == 'y' || ans == 'Y');
    cout << "We produce " << count << " calculations";
}

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