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
#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;
}
Comments
Leave a comment