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() {
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";
}
Comments
Leave a comment