C++ Homework Help: Calculate Product of Numbers

In summary, the conversation was about a programming assignment where the user needs to enter a number between 3 and 10. The goal is to use a loop to calculate the product of the numbers between 2 and the user's input. The code provided works for inputs 3 through 5, but has issues with inputs above 5. The expert has provided a revised code that fixes the issue and produces the correct product.
  • #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:

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:
Technology news on Phys.org
  • #2
I take it you finished your homework... In any case:

C++:
#include <iostream>
using namespace std;

int main (){
    int userInt = 0;
    cout << "Please enter a number from 3 to 10: ";
    cin >> userInt;
    cout << userInt<< endl;

    if (!(userInt >= 3 && userInt <= 10)) {
       cout << "Please follow the directions!" << endl;
    } else {

    cout << "2";
    uint multiplier = 2;
    uint result = userInt;
    while (userInt > 2) {
        cout << " x " << ++multiplier;
        result *= (userInt - 1);
        userInt--;
    }

    cout << " = " << result << endl;
}

    return 0;
}
Compile, debug and run here.
 

FAQ: C++ Homework Help: Calculate Product of Numbers

What is C++ Homework Help: Calculate Product of Numbers?

C++ Homework Help: Calculate Product of Numbers is a service that provides assistance to students who are struggling with their C++ programming assignments involving calculating the product of numbers.

What kind of help can I expect from C++ Homework Help: Calculate Product of Numbers?

Our service offers step-by-step guidance and explanations on how to calculate the product of numbers using C++ programming language. We can also provide sample code and debugging assistance if needed.

Who can benefit from C++ Homework Help: Calculate Product of Numbers?

Our service is designed for students of all levels who are studying C++ programming and need help with their homework assignments. This can include high school, college, and university students.

How do I submit my C++ homework for help with calculating the product of numbers?

You can submit your homework by filling out our online form and uploading your assignment instructions and any relevant files. Our team will review your request and provide a quote for the service.

Is C++ Homework Help: Calculate Product of Numbers a legitimate service?

Yes, our service is completely legitimate. We have a team of experienced and qualified programmers who are dedicated to helping students with their C++ homework. We also guarantee confidentiality and plagiarism-free work.

Similar threads

Replies
3
Views
3K
Replies
5
Views
2K
Replies
23
Views
2K
Replies
6
Views
10K
Replies
22
Views
3K
Back
Top