Need help with a factorial program

I'm just starting and i need help with this problem:

Write a recursive function Factorial() that returns the factorial (a long) of its int argument. The factorial of a positive integer is the product of it and all the positive integers less than it. For example, the factorial of 5 is 5*4*3*2*1 or 120. The symbol for factorial is the exclamation point, therefore 5!=120. Computing this factorial can also be thought of as 5!=4! or more generally, N!=N*(N-1)! By definition, 0! is equal to 1.

Any help would be great. Thanks.

Comments

  • Hi,

    Something like this:

    [code]
    #include

    long factorial(long a)
    {
    if (a > 1)
    return (a * factorial(a-1));
    else
    return (1);
    }

    int main()
    {
    // Declaring the variables
    long l;
    std::cout << " Please enter a positive number: ";
    std::cin >> l;

    std::cout << l << "! = " << factorial(l) << std::endl;

    return 0;
    }
    [/code]

  • Hey buddy well i'll give you something to start on ok

    long Factorial( long& n)
    {
    // test if n is 0 , then you have to return 1 right?

    else
    return n times myself(n-1) // recursive call here
    }

    not exactly the most efficient way to do it but it's probably what your
    learning now.

    hope it helps,
    Mel


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories