Factorial Program in C
The factorial of a number, denoted as 'n!', is the product of all positive integers up to n. Essential in permutations, combinations, and probability, it simplifies complex calculations, like determining possible seating arrangements
Algorithm of C Program for Factorial
Let us go through the algorithm of C program to find factorial of a number
- Take input the number n from the user whose factorial is to be calculated.
- We start from n, decrease its value to 1, and keep multiplying the value achieved at each stage.
- The final value obtained when n becomes 1 is equal to our factorial.
Factorial Program Using For Loop
Now we shall see how to write a program to find the factorial of a number.
To calculate the factorial of a number n, we have to multiply all the numbers from 1 to n. We can do this with the help of a for loop. We use a variable called fact in which our factorial will be stored. We initialize fact as 1.
We run a for loop from i = 1 to i <= n, and at each step, multiply i with our fact variable. We then print our fact variable to the screen.
Input : 5
Output: Factorial of 5 is: 120
Explanation:
Let us understand the code with the help of the given example. Here the input is 5. We are using a for loop to traverse from i = 1 to i <= 5, and at each step, i is multiplied with our fact variable.
- When, i = 1, fact = 1.
- When i = 2, fact = 1 X 2 = 2.
- When i = 3, fact = 2 X 3 = 6.
- When i = 4, fact = 6 X 4 = 24.
- When i = 5, fact = 24 X 5 = 120.*
In this way, we are calculating our factorial using for loop. If the given number is n, our loop will execute n times. Thus, the time complexity is O(N).
C Program for Factorial Using While Loop
We can also use a while loop instead of a for loop to write a program to find factorial of a number in this we loop through all the numbers from one to n and calculate the factorial. We use a variable called fact in which our factorial will be stored. We initialize fact as 1. We also use another variable i, and initialize i as 1.
We run our while loop while i <= n, and at each step, multiply i with our fact variable. The value of i is incremented by 1 at each step. We come out of the while loop and print our fact variable to the screen.
Input : 5
Output:
Explanation: Let us understand the C program to find factorial of a number with the help of the given example. Here the input is 5. At first, our i is set to 1. We are using a while loop at each step of which i is multiplied by our fact variable and incremented by 1.
*When, i = 1, fact = 1, i becomes 2.
- When i = 2, fact = 1 X 2 = 2, i becomes 3.
- When i = 3, fact = 2 X 3 = 6, i becomes 4.
- When i = 4, fact = 6 X 4 = 24, i becomes 5.
- When i = 5, fact = 24 X 5 = 120, i becomes 6.
- i is no longer less than equal to 5. Thus, we come out of the while loop and print 120 to the screen.
In this way, we are calculating our factorial using a while loop.
If the given number is n, our loop will execute n times. Thus, the time complexity is O(N).
Factorial Program Using Recursion
Now let's see the factorial program in C using recursion. The function will keep on calling itself until a base condition is reached. Let us see how to write a program to find the factorial of a number.
We use a recursive function and pass the number whose factorial is to be calculated, say n. The recursive function will call itself with descending values, n-1, n-2, n-3, and all the way till 1. At each step, it will multiply the values to calculate the factorial.
Input : 4
Output:
Explanation:
Let us understand the above C program to find factorial of a number with the help of the input. If we are to calculate the factorial of 4, this is how the flow of the recursive call will be:
fact(4) = 4 X fact(3) = 4 X 3 X fact(2) = 4 X 3 X 2 X fact(1) = 4 X 3 X 2 X 1 X fact(0) = 4 X 3 X 2 x 1 //since fact(0) will return 1. = 24
In this way, we are using recursion to calculate the factorial of a number in C.
The recursive function will be called n-1 times, and at each time, the value of n is decremented by 1. Thus the time complexity of the above program will be O(N).
C Program for Factorial Using Ternary Operator
The ternary operator is a shorthand way of using if-else. It takes three arguments:
- The comparison argument
- The result value if the comparison is true
- The result value if the comparison is false
Let us see how we can use the ternary operator to write the factorial program in C.
- We shall check if n equals 1 or 0.
- If it is true, we shall return 1.
- If it is false, we shall return n X fact(n-1).
This can be done by the following single line of code: return (n == 1 || n == 0) ? 1 : n factorial(n - 1);
The flow of this program would be similar to the recursive program. Only the if condition of the recursive function is replaced with the ternary operator.
Input : 4
Output:
Explanation:
We have already seen the flow of the recursive function. The flow of the factorial program using c ternary operator is similar. At each step, the function will check if n is equal to 0 or 1. If so, then it will return 1, else it will return n X fact(n-1). Since the function is running N times, the time complexity of the above program will be O(N).
Conclusion
- Factorial of a number is the product of all positive numbers smaller than or equal to it.
- Factorial of 0 and 1 is 1.
- Factorial is represented by the ! sign.
- We can calculate factorial in C using various methods, the most popular of them being making the usage of for loop, while loop, recursion, and ternary operator.