Recursion Lab Part 2

Welcome to Recursion Lab Part 2. In this lab you will write an application that will calculate the factorial of a number. If you do not have your IDE already open, do so and create a new workspace and file. As stated at the beginning of this lab you will be writing an application that will calculate the factorial of a number that the user enters.

Before we begin we need to think about what needs to be done to calculate the factorial of a number. As you may already know, in order to calculate a factorial, you need to multiply the results of the exponentially decreasing number. For example, if you wanted to find the factorial of 4 then your formula would look similar to this: 4*(4-1)! Lastly, you should also keep in mind that 1 should be smallest number – multiplying by zero will cause the result to be zero.

Now that we know how to calculate the factorial of a number, it is time to code up the application. Go ahead and type in your application skeleton, prompt the user for the number of which to calculate the factorial, and declare an integer to hold that input. Your code should look similar to the following:

void main ()
{
     int num = 0;
     cout<<"Enter number to find the factorial of: ";
     cin>>num;
}

Create a procedure to do the calculations and pass in the input from the user.  Your code should look similar to the following which has a prototype of the function which is created beneath main:

#include <iostream>

using namespace std;

int multi (int num);

void main ()
{
     int num = 0;
    cout<<"Enter number to find the factor of: ";
    cin>>num;
}
int multi (int num)
{
}

Now that you have the basic framework for your procedure, let’s put in the “filling”. Since we are going to use recursion, there must be a terminating condition the procedure must work towards it. In this case, the terminating condition will be when the num reaches zero. The following code is the recursive step of your function which also works towards the terminating condition:

num * multi(num-1);

The above line of code, as you can see, multiplies the number given by the user, num, by the function itself – calling the function again.  The following code sets up the terminating condition and moves closer to it.  In this case, the terminating condition is when num = 0, as show in the conditional statement (if statement).

#include <iostream>

using namespace std;

int multi (int num);

void main ()
{
     int num = 0;
     cout<<"Enter number to find the factorial of: ";
     cin>>num;
}
int multi (int num)
{
     if (num > 0)
     {
     }
     else
     {
     }
}

To move closer to the terminating condition we will use the aforementioned code, num * multi(num-1);. As you might have guessed this code should be within the brackets of the if-statement. This code calls the function again and subtracts one from num so that the terminating condition can be reached. A good idea may be to store the result of this calculation into a variable. Now that we know what kind of code should be within the conditional statements it is time to enter this code:

#include <iostream>

using namespace std;

int multi (int num);

void main ()
{
     int num = 0;
     cout<<"Enter number to find the factorial of: ";
     cin>>num;
}
int multi (int num)
{
     int ans = num;
     if (num > 0)
     {
          ans = num * multi(num-1);
          return ans;
     }
     else
     {
          return 1;
     }
}

Notice that 1 is returned in the else statement. That tells the procedure to end and will occur when num = 0. Compile and run your code. If there are no errors, let’s move on to the next step – give the user the result of the calculations. This means that you will need to:
     1. Call the procedure, multi
     2. Display the result to the user
As shown in this code:

#include <iostream>

using namespace std;

int multi (int num);

void main ()
{
     int num = 0;
     cout<<"Enter number to find the factoral of: ";
     cin>>num;
     cout<<"The factor is "<<multi (num)<<endl;
}
int multi (int num)
{
     int ans = num;
     if (num > 0)
     {
          ans = num * multi(num-1);
          return ans;
     }
     else
     {
          return 1;
     }
}

Your output should look similar to this:

Congratulations you have now completed your factorial application.