Introduction
In this factorial program in c, we will read a number from the user and then, we will display its factorial value. So, the question is what is factorial of a number?
Factorial of a number is nothing but a multiplication from 1, 2, 3 up to that number. Suppose, I need to find factorial of 5, then its answer is 120. This value is coming from the expression 1*2*3*4*5. Using the same logic, factorial of a 3 is 6 and that of 10 is 3628800.
Also Read: Interview Questions On C
So, here we are writing a program in c to find factorial of a number. I will write four c programs for the same output using for loop, while loop, do-while loop and recursion. The program will remain the same, only a certain part will be different from one another.
Before writing a program in c to find factorial of a number, just see its output.
Output of Factorial Program in C
Enter any number
5
Factorial of 5 is 120
You can see the above output. It will remain same for all the four c programs.
Also Read: Perfect Number in C Programming using All Loops
Factorial program in c using for loop
In this program, I am finding factorial of a number using for loop.
#include<stdio.h>
int main()
{
int fact=1,i,n;
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = fact * i;
}
printf("\nFactorial of %d is %d", n,fact);
return 0;
}
Explanation
int fact=1,i,n;
This is the variable declaration part. The variable fact will store factorial of a given number and its initial value is 1. I have initialized this value 1 because we are going to multiply this variable fact with the variable i up to n. The variable i is a loop variable and variable n is used to store the value which is read from the console.
Also Read: Strong Number in C Programming
Suppose we have read the value of n=5, then next part i.e. loop will be executed.
for(i=1;i<=n;i++)
As you can see this is for loop statement and it contains three expressions. First expression is i=1. As I have already mentioned that for finding factorial of a given number, we need to start multiplication from the value 1 to the value of n. So, that is why, we have initialized the value of i to 1. In the for loop, expression 1 is executed only once. So, this i=1 will be initialized only once.
Next expression is i<=n. This is the second expression in the for loop. On the basis of this expression, we will decide whether to execute the body of for loop or not. Why this expression is written? This expression is checking the value of i must not exceed the value of n. Because the reason is we need to find factorial of n, therefore, we are strictly checking here the condition i<=n. Because, if the value of i becomes greater than n, that means we got our factorial and the loop will be terminated here.
Also Read: Floyd Triangle in C Programming
But, if this condition is true i.e. the value of i is less than n, then we will execute the body of the for loop.
In the for loop statement, third expression is i++. We are incrementing the value of i by 1. Why? Because, we need to do multiplication of 1*2*3……*n. So, every time we need the value of i incremented by 1. But, this expression will be executed only after the execution of the body of for loop.
Body of for loop
{
fact = fact * i;
}
The above body of for loop means the statements that we need to repeat enclosed inside the curly brackets. So, here is only one statement is written and that will calculate factorial of a given number. How many times this body of for loop will be executed? This depends on all the three expressions in the for loop statement. If the value of i is 1 and that of n is 5 and if we are incrementing the value of i by 1, that means this loop will be executed 5 times.
Suppose, n=5 then the value of fact will be 120.
I hope, you understood this program very well. There is same logic for the while and do-while loop.
Factorial program in c using while loop
#include<stdio.h>
int main()
{
int fact=1,i=1,n;
printf("Enter any number\n");
scanf("%d",&n);
while(i<=n)
{
fact = fact * i;
i++;
}
printf("\nFactorial of %d is %d", n,fact);
return 0;
}
Factorial program in c using do-while loop
#include<stdio.h>
int main()
{
int fact=1,i=1,n;
printf("Enter any number\n");
scanf("%d",&n);
do
{
fact = fact * i;
i++;
}
while(i<=n);
printf("\nFactorial of %d is %d", n,fact);
return 0;
}
All these three factorial programs in c will display same output. As you can see, there is only change in syntax in the loop statement.
Also Read: Palindrome in C using Pointers
Now, this same program can be done using recursion. Now, this is a very important concept.
Recursion in C Programming
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. This recursive function has the keyword return which passes back the result to the original caller function.
Factorial using Recursion in C
#include<stdio.h>
int fact(int);
int main()
{
int f, n;
printf("Enter any number\n");
scanf("%d",&n);
f = fact(n);
printf("\nFactorial of %d is %d", n,f);
return 0;
}
int fact(int num)
{
if(num==1)
{
return(1);
}
else
{
return(num*fact(num-1));
}
}
In the above program, we have one function i.e. fact(). From the main function, we are calling the fact() function with one argument. In this function, we have used the if-else statement. In the if statement, we are checking, if the value of num = 1 then it will return 1 to the function from where it is called. But, in the else statement, we are calling the same function and this is nothing but a recursion.
Also Read: C Program To Print All The Numbers In a Given Range
Suppose the value of num=5 then
fact(5)=5*fact(4)
=20*fact(3)
=60*fact(2)
=120*fact(1);
fact(5)=120
I hope, you have understood how to write the c program to find factorial of a given number. Thanks for reading.