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

Question #323329

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:48:25-0400
#include <iostream>
using namespace std;


int main() {
    int limit;
    int count = 0;
    char ans;


    do {
        cout << "Enter a limit: ";
        cin >> limit;


        int sum = 0;
        for (int i=2; i<=limit; i+=2) {
            sum += i;
        }
        cout << "The sum of even numbers is " << sum << endl;


        int prod = 1;
        for (int i=3; i<=limit; i+=2) {
            prod *= i;
        }
        cout << "The product of odd numbers is " << prod << endl;


        count++;


        cout << endl << "Do you want to terminamte loop? ";
        cin >> ans;
    } while (ans != 'y' && ans != 'Y');


    cout << "Do-loop execute " << count << " times";

    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