Assuming the following variables and code has been written, complete the C++ cod
ID: 3586726 • Letter: A
Question
Assuming the following variables and code has been written, complete the C++ code by writing a do while loop that will calculate the product of all of the values between a lower bound and 6, inclusive. After the loop, display the calculated product. For example, if the user enters 3 as the lower bound, then the loop should multiple 3 * 4 * 5 * 6 to get a product of 360 and then 360 should be displayed. You may assume that the lower bound that the user enters will be less than or equal to 6. Make sure to declare any other variables that are needed
int product, //holds the product of the values
lowerBd; //holds the lower bound of the values to be multiplied
cout << "What is the lower bound? ";
cin >> lowerBd;
Then, REWRITE THE PROGRAM WITH A FOR LOOP AND A WHILE LOOP.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int product=1, //holds the product of the values
lowerBd; //holds the lower bound of the values to be multiplied
cout << "What is the lower bound? ";
cin >> lowerBd;
while(lowerBd <= 6) {
product = product * lowerBd;
lowerBd = lowerBd + 1;
}
cout<<"Product = "<<product<<endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main()
{
int product=1, //holds the product of the values
lowerBd; //holds the lower bound of the values to be multiplied
cout << "What is the lower bound? ";
cin >> lowerBd;
do {
product = product * lowerBd;
lowerBd = lowerBd + 1;
}while(lowerBd <= 6);
cout<<"Product = "<<product<<endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main()
{
int product=1, //holds the product of the values
lowerBd; //holds the lower bound of the values to be multiplied
cout << "What is the lower bound? ";
cin >> lowerBd;
for(int i = lowerBd; i<= 6;i++) {
product = product * i;
}
cout<<"Product = "<<product<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.