HELP!!!!!!! I hate factorials.

I have been banging my head against the wall with this problem since yesterday. I have tried everything. The exercise is:

Write a program that lets a user enter N and that outputs N! (meaning N*(N-1)*(N-2)*...*2*1). Hint: Initialize a variable totalValue to N, and use a loop variable i that counts from N-1 down to 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
#include <iostream>
using namespace std;

int main() {
   int totalVal = 0; 
   int userInt = 0; 
   
   // FIXME: Ask user to input an integer, store in userInt
   
   totalVal = userInt;
   // FIXME: Add while loop that counts down to 1, updating totalVal
   
   cout << userInt <<"! is " << totalVal << endl;

   return 0; 
}
Last edited on
Try writing pseudo code first:

1
2
3
4
5
6
7
8
9
10
11
12

// loop:

//Set totalValue to N:

//Set loop variable i to N - 1:

//while i > 1
//  calculate the next value of N per the algorithm your teacher gave you


This is what I have so far.


#include <iostream>
using namespace std;

int main() {
int totalVal = 0;
int userInt = 0;

cout << "Enter an interger.";
cin >> userInt;// FIXME: Ask user to input an integer, store in userInt

totalVal = userInt;
while (userInt >= 1) {
userInt = userInt -1;
totalVal = userInt * totalVal;// FIXME: Add while loop that counts down to 1, updating totalVal
}
cout << userInt <<"! is " << totalVal << endl;

return 0;
}

I get back 0! is 0
Last edited on
I already told you what to do in your other thread, you just didn't do it...
http://www.cplusplus.com/forum/beginner/159068/
Is N userInt? Or does it have to be set to N literally.
Why would you not know that userInt is N? Didn't you write the code?

Anyway, my second post on that thread told you to change while (userInt >= 1) { to while (userInt > 1) {. That would give you the output

1! is X


Where X is whatever the computed factorial is going to be. In the first post on that thread, I told you that you were going to need a third variable in order to get the correct output (so it doesn't always say "1!").
You could always do it the recursive way like on the tutorial http://www.cplusplus.com/doc/tutorial/functions/



Here's two other ways

Notes:
int will only allow from 0! -> 12!
you will need to do error checking because you can't have negative factorial or overflow ( I didn't do either because those can easily be done with an if statement and are self explanatory)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//simple recursion
int factorial(int n)
{
    return !n ? 1 : n * factorial(n - 1);
}

//simple loop
int factorial = n;
if(!n)
{
    factorial = 1;
}
else
{
    while(--n)
    {
        factorial *= n;
    }
}
Last edited on
I finally got it! Thanks for all the help. I was making it way harder than it is. Man do I feel stupid.
Feel free to post your solution in case someone else has the same issue in the future. Also, there is no need to feel stupid we all had to start from somewhere.
Topic archived. No new replies allowed.