I have a problem with a program I'm writing. It's supposed to find the factorial of the number you input, but it's not. The problem seems to be that the recursive function isn't...well...recursing. Right now, when compiled, the program multiplies the number by the number before it. So if it's 5 that you put in, it gives you 20, instead of 120, which it would if the factorial thing worked.

Don't tell me the whole answer to the problem; it would be nice if you could just point me in the right direction.

Oh, and please remember that I'm a novice. I've only been coding lightly for about 3 or 4 months, perhaps.

Recommended Answers

All 9 Replies

And I guess it would help if I put the code in here...

#include <iostream>
using namespace std;
void recurse(int input) //the recurse function...
{
     if (input == 0){ //if the input becomes 0, exit
               return;
               }
     recurse(input - 1); //calls itself
}
int main()
{
    int input;
    
    cout<<"Input a number to find the factorial of:\n";
    cin>>input; //get input value
    cin.get();
    
    recurse (input); //calls recurse function
    cout<<input * (input-1)<<"\n"; //multiplies it all together
    cin.get();
}

Two problems:
1) need a return value to return the factorial. Your function does the recursion but doesn't calculate anything.

2) The function's return value should be int, not void.

You're not modifying the variable, just taking it as a parameter.
Do you have to use recursion and not a loop?

cout<<input * (input-1)<<"\n"; //multiplies it all together

that line is not dependent on the function call before it.

also, you should call your function "factorial" instead of "recurse". The recursiveness should be implied by the behavior, it shouldn't need to be stated explicitly.

Dave

I think the problem is that I don't know how to take the output of a function and use it...

If a function returns, say, an int, you can use a call to that function in every place you could have used an int.
Examples:

#include <iostream>

int sumFunction(int a, int b) {
     return a + b;
}

int main() {
     int a = 5, b = 7, c = 0;
     c = sumFunction(a, b); // c now is 5 + 7 = 12
     b = a + sumFunction(a, c); // b now is 5 + (5 + 12) = 22;
     std:cout << "123 + 123 = " << sumFunction(123, 123) << std::endl;
     return EXIT_SUCCESS;
}

So if you want to evaluate n!, your function should return that number for you to do whatever you like with it.
The purpose on making it recursive is that you know a base case (0! = 1 by definition) and you have n! = (n-1)! * n (a recursive definition of the factorial).

Combine the two things and remember to output the result.

int factorial(int n) {
     if(n==0) {
          return 1; // 0! = 1
     }
     return /* I leave it up to you what to return here. Hint: it should contain a call to factorial(int n) itself (hence recursion) */
}

Hope this helps.

I think the problem is that I don't know how to take the output of a function and use it...

int iReturn = recurse (input);

or 

int iReturn = 0;

iReturn = recurse (input);

Also, your factorial calculation should be done inside the recursive function.

I finally got this one! YES!!! Thanks everyone, I'm so ecstatic now. This thread is finally solved, I finally understand recursion, etc. etc. Hooray! And I was just beating myself up five minutes ago because I couldn't get it. Finally...yes...I feel so good about myself now. Thanks everyone for your help. Here's the completed program:

#include <iostream>
using namespace std;

int factorial(int x);

int main(){
    int x;
    
    cout<<"Input a number to find the factorial of:\n";
    cin >> x;
    cin.ignore();
    
    cout<<"The factorial of " << x << " is " << factorial(x) << "\n";
    cin.get();
}
int factorial(int x){ 
    if (x == 0){
          return 1;
          }
    return x * factorial(x - 1); //it took me MONTHS to get THIS?!?!
}

New problem though...(i guess it's not really a problem, just something weird) When i put in 20 to find the factorial, I get -2102132736...now I know it's not a negative number. Is this just an inherent bug in the program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.