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(void)
{
int limit;
int count;
char c;
double sumEven, prodOdd;
do
{
cout << "Please, enter a limit: ";
cin >> limit;
sumEven = 0;
prodOdd = 1;
count = 0;
for (int i = 0; i <= limit; i++)
{
if (i % 2 == 0)
sumEven += i;
else if (i % 2 == 1)
prodOdd *= i;
count++;
}
cout << "Sum of even is " << sumEven
<< "\nProduct of odd is " << prodOdd
<< "\nUser run do-while loop " << count << " times";
cout << "\nThe program should be terminated or not? [Y/N]: ";
cin >> c;
} while (c=='N');
}
Comments
cool
Leave a comment