Factorial in C programming language is mostly used to demonstrate the application of While Loop, For Loop and Recursion. Actually, factorial series has many real-life uses and is used to solve problems of permutation and combinations. In this article, we try to understand what is factorial series. What are the uses of factorial series? And how to write a factorial program in the C Programming language
In this blog, we will understand how to write Factorial Logic using 3 Different methods
- Find Factorial of a Number using While Loop
- Find Factorial of a Number using For Loop
- Find Factorial of a Number using Recursive Function
First Understand what is Factorial Series and its Uses
Factorial Series is denoted by an exclamatory Symbol (!) like this. Factorial Series is the multiplication of any natural number to all the numbers smaller than it. So the mathematical representation of the factorial series will be like this
n! = n * (n-1) * (n-2) *(n-3)*………..*3*2*1
Factorial Series is used in solving many mathematical problems like Permutation and Combination.
This permutation and combination are further used in complex analysis programming like AI and reporting.
Well, you can say the Factorial series is used to solve many other complex series and operations.
Find Factorial of a Number using While Loop
While Loop is very important in every programming language. Apart from the while loop, you need knowledge of variables, print, and scanf statements. If you don’t know what is C Programming or printf and scanf statement. Then you need to go back to books and refer to an online course.
If you are in the US you can watch the C programming course on Amazon Prime or and you can also watch the C Programming series in the UK as well. Amazon Prime C Programming series is best, free(For Amazon Prime Member), and very simple to understand. If you are not in US and UK then you should subscribe to the course of C Programming at Newtum.
main() { int f , i , n; // Declaring Variables printf("Please Input the Number\n"); scanf("%d",&n); f = 1; // Initializing Variables i=1; // Initialing Counter Variable while(i <= n) { f = f * i; i++; } // Displaying the final output printf("Factorial of Number %d is %d \n\n",n,f); }
Output:
Please Input the Number
3
Factorial of Number 3 is 6
Detail explanation of this Factorial Program using a While loop is provided at the bottom, in the how-to section of this blog.
Find Factorial of a Number using For Loop
For Loop is very important in every programming language. It’s being used to write complex and nested loops. If you don’t understand for loop then you should learn it for our other blog or courses.
If you are in the US you can watch the C programming course on Amazon Prime or and you can also watch the C Programming series in the UK as well. Amazon Prime C Programming series is best, free(For Amazon Prime Member), and very simple to understand.
If you are not in US and UK then you should subscribe to the course of C Programming at Newtum.
main() { int f , i , n; // Declaring Variables printf("Please Input the Number\n"); scanf("%d",&n); f = 1; // Initializing Variables for(i=1;i<=n;i++) { f = f * i; } // Displaying the final output printf("Factorial of Number %d is %d \n\n",n,f); }
Output :
Please Input the Number
5
Factorial of Number 6 is 120
Detail explanation of this Factorial Program using For loop is provided at the bottom, in the how-to section of this blog.
Find Factorial of a Number using Recursive Function
The function is a wast topic and it’s not in the scope of this blog. You can always refer to our course for C Programming if you want to understand functions in detail.
main() { int f , i , n; // Declaring Variables printf("Please Input the Number\n"); scanf("%d",&n); f = 1; // Initializing Variables f=factorial(n); // Displaying the final output printf("Factorial of Number Using Recursion %d is %d \n\n",n,f); } int factorial(int x) { int temp; if(x > 1) { temp = factorial(x-1); return x *temp; } else{ return 1; } }
Output:
Please Input the Number
6
Factorial of Number 6 is 720
How to Section explains each program of the above C Programs in detail
Time needed: 15 minutes
How to write Factorial C Program in 3 Different Ways
- Factorial of a Number using While Loop
1 . Declare 3 Variables “n” for input number, “i” for counter and “f” for storing the final results.
2. Input the number using scanf statements and above that write a printf statement so that the user will understand.
3. Initialize the variable i and f to 1, it’s necessary because factorial involves multiplication and the value cannot be zero.
4. Write while loop with condition i <= n, so that our loop will run n times.
5. Inside the while loop writes multiplication logic f = n* i, so that every time value of i is incremented multiplication will be done.
6. Increase the value of i every time loop is executed using the increment operator.
7. After the while loop print the value of the “f” variable. - Factorial of a Number using For Loop
1. First we will declare 3 Variables “n” for input number, “i” for counter and “f” for storing the final results.
2. We will ask the user to input the number with the help of printf statement and scanf statement.
3. Initialize the “f” variable with the value 1.
3. Write for loop, with i = 1 and condition i < = n
4. Inside for loop write multiplication logic f = n* i, so that every time value of i is incremented multiplication will be done.
5. After the for loop print the value of the “f” variable.
- Factorial of a Number using Recursive Function
1. First we will declare 3 Variables “n” for the input number, “i” for the counter, and “f” for storing the final results.
2. We will ask the user to input the number with the help of printf statement and scanf statement.
3. Initialize the “f” variable with the value 1.
3. Call the function factorial while passing the value as “n”.
4. After the for loop print the value of the “f” variable.
5. Declare the function factorial in the function, accepting input in the variable “x”.
6. Inside the function factorial declare a variable temp.
7. Write the if and else statement for x > 1 and inside the condition write the required expression as shown in the code and return the value. Here we are calling the same function again till the condition is met. This is recursion.
8. In the else condition write return 1.