Determine the Factorial of a Number by Using Recursion
We can get the factorial of any number by the use of the recursion method. In this example, we take any number from the user and then compute the factorial of that given number. We include a header file as follows.
using namespace std;
int factorial(int x);
int main()
{
int x;
cout <> x;
cout << "Factorial of " << x << " = " < 1)
return x * factorial(x - 1);
else
return 1;
}
Suppose we enter the number “12” in the program as required, the factorial() method takes this integer only as an argument. This function multiplies 12 by the factorial of (12-1 = 11). To do this, the number “11” is passed back to the factorial() method. Similarly, in the next repetition, multiply 11 by the factorial of (11-1 = 10). The factorial() method is then given 10 as a parameter. This process lasts till the value extends 1 and returns 1. Now, every function gives back a value to calculate factorial which is reverted to the main() function.
The user enters a number 12 and then obtains the factorial of this number.
Use For Loop
We determine the factorial of an integer with the help of the “for” loop. Let’s have a look at what’s going on in the code below. We include the header file at the start. After this, we use the namespace std. We have declared three variables. Variables ‘a’ and ‘j’ are of integer data type. Then, we utilize the cout function to get a positive integer from the user. That integer is stored in variable ‘a’. Here we enter number ‘9’.
using namespace std;
int main()
{
int a, j, factorial=1;
cout<>a;
for(j=a; j>=1; j--)
factorial = factorial*j;
cout<<"\nFactorial = "<<factorial;
cout<<endl;
return 0;
}
Now, after getting the integer from the user, the appraisal of the “for” loop will start. The value of variable ‘a’ (9) is initialized with variable ‘j’. Therefore, j = 9. The initialization section of the for loop is implemented first, but just once. The state j> = 1 or 9> = 1 assesses to true, so the flow of the program goes into a loop and factorial * j or 1 * 9 or 9 is initialized to factorial. The flow of the program goes to the third part of the loop and the value of ‘j’ is decremented. Therefore, j = 4. Condition j> = 1 is appraised again. The condition is assessed each time and the value of ‘j’ is updated. And after each evaluation of the condition, the program inside the loop executes till the condition assesses to false. So, when the condition is false, we get the values ‘j’ and ‘factorial’ after every assessment. The value of ‘factorial’ is retrieved at the end of the loop. And this will result in the factorial of the specified number.
The user inputs the number ‘9’ and then presses the Enter key to get the factorial of 9.
Use While Loop
Another method to find the factorial value is the use of a while loop. In this code, we include the header file <iostream>. We take the variable “m” as having a data type integer. We have also applied the “if” condition. It returns 1 if the value of ‘m’ is equivalent to 0. After this, we take more variables. We need to initialize these variables before the while loop.
using namespace std;
unsigned int factorial(unsigned int m)
{
if(m==0)
return 1;
int k = m, factorial = 1;
while (m / k != m) {
factorial = factorial * k;
k--;
}
return factorial;
}
int main()
{
int n = 16;
cout << "Factorial of the number "
<< n<< " is "
<< factorial(n) << endl;
return 0;
}
We utilize the while statement to implement an iterative structure when the operations are unknown, and the iteration lasts till the test condition becomes true. Once the while statement is implemented, the computer first assesses the given test condition. If that condition is true, the body of the loop is implemented. After running the loop body, the test condition is assessed again, and if true, the loop body is run once more. This procedure continues till the given test condition is false. If false, control is transmitted to the first declaration after the end of the loop body. The body of the loop contains only one statement or multiple statements.
Next, we take the number ‘16’ to find the factorial. This number is signified by the variable ‘n’.
After running the above-mentioned code, we get the factorial of 16 in output.
Conclusion:
This article explains the techniques of finding the factorial of a number. We multiplied all the non-negative integers which are less than or equivalent to that number to get the factorial of that number. For non-negative numbers, the factorial is always determined. To get the factorial of positive integers, we use the for loop and the while loop. We also utilized the recursion method for this purpose.