C++ Homework Help: Calculate Product of Numbers

  • Context: C/C++ 
  • Thread starter Thread starter dnc1786
  • Start date Start date
  • Tags Tags
    C++ Homework
Click For Summary
SUMMARY

The discussion focuses on a C++ assignment requiring users to calculate the product of numbers from 2 to a user-defined integer between 3 and 10. The initial code provided by the user fails to compute the correct product for inputs greater than 5 due to incorrect variable manipulation within the loop. A revised version of the code is suggested, correcting the logic and ensuring accurate calculations by properly managing the multiplier and result variables.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with loops, specifically while loops in C++
  • Knowledge of variable types and arithmetic operations in C++
  • Basic debugging skills in C++ programming
NEXT STEPS
  • Study C++ loop constructs, focusing on while and for loops
  • Learn about variable scope and lifetime in C++
  • Explore debugging techniques in C++ using IDEs like Code::Blocks or Visual Studio
  • Practice writing functions in C++ to modularize code for better readability and maintenance
USEFUL FOR

Students learning C++ programming, educators teaching programming concepts, and anyone looking to improve their coding skills in C++ through practical assignments.

dnc1786
Messages
1
Reaction score
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
I take it you finished your homework... In any case:

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

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

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

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

    count << " = " << result << endl;
}

    return 0;
}
Compile, debug and run here.
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
1
Views
8K