Question: Language: C++ Write a program that asks the user for non-negative integers X and Y. Using a loop, alternate between adding and multiplying integers starting

Language: C++

Write a program that asks the user for non-negative integers X and Y. Using a loop, alternate between adding and multiplying integers starting at X and finishing at Y. If the number is even, add it to the total. If the number is odd, multiply it. For example, if X=5 and Y=10, your program should calculate 5+6*7+8*9+10=775. If X=2 and Y = 5, calculate 2*3+4*5=50.

(Ignore mathematical order of operations, just perform the calculations in order as the number increments. Do NOT do the multiplication before addition.)

Output the result with a cout statement.

Here is the code I was attempting. The numbers it outputs is far too large.

#include using namespace std;

int main(){ int X, Y; int total = X; cout << "Enter a non-negative integer for X: "; cin >> X;

cout << "Enter a non-negative integer for Y: "; cin >> Y;

int i = X;

while (i <= Y){ if (i % 2 == 0){ total = total + i; i++; } else{ total = total * i; i++; } } cout << total << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!