The factorials in C is represented with an exclamation mark symbol (!) at the end of N. For example, Let us find the factorial of 7, and then we will write it as
Factorial of 7: 7! = 7 X 6 X 5 X 4 X 3 X 2 X 1 = 5040
Here, we will call 7! are "7 factorial”. All the numbers less than or equal to the given number N are added together in descending order to find the factorial. Suppose we want to find the factorial of N. Then the general formula to find the factorial of any number N is given by
N! = N * (N-1) * (N-2) * (N-3) *............. * 3 * 2 * 1.
In this post, we will learn to write a program to find the factorial using C language. Read the complete article to know the naive as well as the optimal approach to solving the problem.
What are Factorials in C ?
The factorial is denoted by N!. It is the product of all positive integers from 1 up to N. Hence, the repeated addition of a number in descending order up to N.
N! = N * (N-1) * (N-2) * (N-3) *............. * 3 * 2 * 1.
For example:
0! = 1
1! = 1
2! = 2*1 = 2
3! = 3*2*1 = 6
and so on.
Factorials are used in many places in mathematical calculations and can be seen mostly in permutation and combination problems.
Factorials in C Problem Statement
Given a number N, find the factorial of the number. In this problem, we have to find the factorial of a given number N. We can find the factorial of a given number using the two best approaches.
- Factorial using loop
- Factorial using recursion
We will learn both methods of solving factorial problems using C language. Read the complete article to understand the problem statement and the solution better.
Also read: Linear Search Algorithm in C
Finding Factorials in C
Let us check the algorithm for finding the factorial of a positive number using the C language.
- First, we need to ask the user to enter the number to find its factorial.
- Now, we will read the integer using the scan function in the C language and assign it to a variable.
- After this, we can use the loop or recursion method to find the factorial of a number.
- Using a loop, we need to iterate up to the length of the number until it does not exceed the number.
- Then, we will keep adding the number until it exceeds N.
- Using recursion, we only need to define a base function such that when the value of N becomes one, we need to return 1.
- We will keep calling the function n* factorial (n-1) until the base condition occurs.
Let us learn about these two methods and their implementation in detail below.
Also read: Keywords and Identifiers in C
Pseudocode To Find The Factorial in C
Let us check the step-by-step pseudocode approach to find the logic for solving the factorials in C .
| Factorial Pseudocode |
| 1. Start
2. Declare variables N (input number), factorial (result)
3. Read input into n
4. Set factorial to 1
5. Repeat until n is greater than 1:
1. Multiply factorial by n
2. Decrease n by 1
6. Print factorial as the result
7. End |
Also Read: What are Array in C
Factorials In C Using Loops
Let us find the factorials in C using for loops. In this method, we will use a loop that will run to (N-1) times to find the factorials in C. Check out the C program below.
| Factorial in C using loops |
| #include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact = fact * i;
}
printf("Factorial of %d is: %d", number, fact);
return 0;
} |
In the above program, we use loops to find a number's factorial. We use a fact variable for loops from 1 to N to store the factorial for every iteration. And after completion of the loop, we print the result. Let us check the output for a given number 7.
Output
| Enter a number: 5
Factorial of 7 is: 120 |
Time and Space Complexity Using loops
Let us find the time taken to find the factorials in C using loops and the memory used in the method.
| Factorials in C using loops |
| Time complexity: O(N)
Space Complexity: O(1) |
Factorials in C Using Recursion
With the help of this method, we can find the factorial using recursion. Here, the recursion function will be called up to N times. And we will keep on decreasing the number each time by 1.
Remember, in recursion, it is mandatory to give a base condition to stop the recursion at some point. Failing to allow a proper base case will result in infinite recursion calls. Here, our recursion call will be when N = 1.
| Factorials in C using Recursion |
| // C program to find the factorial of a given number
#include <stdio.h>
unsigned int factorial(unsigned int n)
{
if (n == 1)
{
return 1;
}
return n * factorial (n - 1);
}
// Driver code
int main()
{
int num = 7;
printf ("Factorial of %d is %d", num, factorial(num));
return 0;
} |
In this program, we use recursion to find the factorial of a given number. We call the recursion function N * factorial (N*1). It runs until the value of N becomes 1.
Output
Time and Space Complexity Using Recursion
Let us check the time the recursion code takes to execute and the memory used in the process. Recursion uses
| Factorials in C using Recursion |
| Time complexity: O(N)
Space Complexity: O (N) |
Factorial Of A Number Using Function In C
Let us find the factorial using a function in C. In this method, we will define a function to find the factorial of the given number.
| Factorial using Function |
| #include<stdio.h>
int findFactorial (int x );
int main( ){
int x,fact,n;
printf ("Enter a number to get factorial: ");
scanf ("%d",&n);
fact = findFactorial (n);
printf (" Factorial of %d is: %d",n, fact);
return 0;
}
int findFactorial ( int n )
{
fact=1;
for (int i = 1; i<= n; i++)
Fact = fact * i;
return fact;
} |
In this method, we simply define a function to find the factorials in C. However, we use a loop inside our function to find the factorial of the given number.
Output
| Enter a number to get factorial: 7
Factorial of 7 is 5040 |