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

  • Thread starter Thread starter dnc1786
  • Start date Start date
  • Tags Tags
    C++ Homework
AI Thread Summary
The discussion revolves around a programming assignment that requires calculating the product of numbers from 2 up to a user-defined integer between 3 and 10. The initial code provided works correctly for inputs 3 to 5 but fails for higher values due to an error in how the product is calculated within the loop. The user is guided to adjust their logic to ensure that the product accumulates correctly as the loop iterates through the numbers. The revised code suggests using a different approach to manage the product calculation and ensure the output format matches the expected result. Key points include the importance of correctly initializing and updating the product variable, as well as ensuring the loop iterates through the correct range of numbers. The discussion emphasizes debugging and refining code to achieve the desired functionality.
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;
    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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top