- #1
dnc1786
- 1
- 0
I am not looking for an answer, just guidance...
My assignment is to ask the user for a number between 3 and 10 (inclusive). Using a loop, calculate the product of the numbers between 2 and that number. For example, if the user chooses 5, the result would be 2 x 3 x 4 x 5 = 120.
Sample run: When input is:
5
The output exactly matches
Please enter a number from 3 to 10: 5
2 x 3 x 4 x 5 = 120
So far I have come up with this, which works perfectly for input 3 through 5. However, the product is wrong when the input is anything above five and I am completely lost as to what is wrong with the code:
My assignment is to ask the user for a number between 3 and 10 (inclusive). Using a loop, calculate the product of the numbers between 2 and that number. For example, if the user chooses 5, the result would be 2 x 3 x 4 x 5 = 120.
Sample run: When input is:
5
The output exactly matches
Please enter a number from 3 to 10: 5
2 x 3 x 4 x 5 = 120
So far I have come up with this, which works perfectly for input 3 through 5. However, the product is wrong when the input is anything above five and I am completely lost as to what is wrong with the code:
Code:
#include <iostream>
using namespace std;
int main (){
int userInt = 0;
int multiple = 2;
int product = 1;
cout << "Please enter a number from 3 to 10: ";
cin >> userInt;
cout << userInt << endl;
if ((userInt < 3) || (userInt >= 11)){
cout << "Please follow the directions!" << endl;
}
else {
cout << "2";
++multiple;
while (multiple <= userInt){
cout << " x " << multiple;
++multiple;
product = product * multiple;
}
cout << " = " << product << endl;
}
return 0;
}
Last edited: