Thread: simplify recursive function

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    48

    simplify recursive function

    I am attempting the challenge at the end of the recursion tutorial on this site to return the factorial of any number.

    I have come up with 2 ways to do it, but both seem a bit cumbersome.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void Recurse(int number,int factorial)
    {
        if (number == 1)
        {
            cout << "The factorial of your number is: " << factorial << ".\n";
            return;
        }
    
        Recurse(number-1,factorial*(number-1));
    }
    
    int main()
    {
        int number;
        cout << "Please enter a positive number:";
        cin >> number;
    
        if (number < 0)
            cout << "You entered a Negative Number!\nExiting...\n";
        else
        if (number == 0)
            cout << "The Factorial of your number is 1.\n";
        else
            Recurse(number,number);
    
        return 0;
    }
    Code:
    #include <iostream>
    
    using namespace std;
    
    // number is the number entered by the user.
    // count is going to increment by 1, starting at 1.
    // factorial is going to hold the product of all numbers from 1 to number.
    void Recurse(int number, int count, int factorial)
    {
        if (number < 0) // return if the input was negative.
        {
            cout << "You Entered a Negative Number.\n";
            return;
        }
        if ((count-1) == number) // This check includes 0 as to return a factorial of 1.
        {
            cout << "The factorial of your number is: " << factorial << ".\n";
            return;
        }
    
        Recurse(number,count+1,factorial*count);
    }
    
    int main()
    {
        int number;
        cout << "Please enter a positive number:";
        cin >> number;
    
        Recurse(number,1,1);
    
        return 0;
    }
    I feel that there is too much code for something so simple, how could I rewrite these to simplify things a little?

    Thanks.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by MattJ812 View Post
    I am attempting the challenge at the end of the recursion tutorial on this site to return the factorial of any number.

    I have come up with 2 ways to do it, but both seem a bit cumbersome.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void Recurse(int number,int factorial)
    {
        if (number == 1)
        {
            cout << "The factorial of your number is: " << factorial << ".\n";
            return;
        }
    
        Recurse(number-1,factorial*(number-1));
    }
    
    int main()
    {
        int number;
        cout << "Please enter a positive number:";
        cin >> number;
    
        if (number < 0)
            cout << "You entered a Negative Number!\nExiting...\n";
        else
        if (number == 0)
            cout << "The Factorial of your number is 1.\n";
        else
            Recurse(number,number);
    
        return 0;
    }
    Code:
    #include <iostream>
    
    using namespace std;
    
    // number is the number entered by the user.
    // count is going to increment by 1, starting at 1.
    // factorial is going to hold the product of all numbers from 1 to number.
    void Recurse(int number, int count, int factorial)
    {
        if (number < 0) // return if the input was negative.
        {
            cout << "You Entered a Negative Number.\n";
            return;
        }
        if ((count-1) == number) // This check includes 0 as to return a factorial of 1.
        {
            cout << "The factorial of your number is: " << factorial << ".\n";
            return;
        }
    
        Recurse(number,count+1,factorial*count);
    }
    
    int main()
    {
        int number;
        cout << "Please enter a positive number:";
        cin >> number;
    
        Recurse(number,1,1);
    
        return 0;
    }
    I feel that there is too much code for something so simple, how could I rewrite these to simplify things a little?

    Thanks.
    you can let your recursion function return an int. something like this would do.
    Code:
    int recurse( int number)
    {
      if( number == 1)
        return 1;
      else
        return number*recurse(number-1);
    }
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM