Howdy readers, today you will learn how to write a program to find the factorial of a number using C Programming language.

The factorial of a number is the product of all integers from 1 to that number. For example: the factorial of 6(denoted as 6!) is 1*2*3*4*5*6 = 720.
We will be using the following methods to find the factorial of a number.
- Using For Loop
- Using While Loop
- Using Functions
- Using Recursion
So, without any delay, let’s begin this tutorial.
C Program to Find Factorial of a Number
C Program
// C Program to Find Factorial of a Number #include <stdio.h> int main(){ int num, fact = 1; // Asking for input printf("Please enter a number: "); scanf("%d", &num); // Calculating Factorial for (int i = 1; i <= num; i++){ fact = fact * i; } // Displaying output printf("Factorial of %d is: %d", num, fact); return 0; }
Output
Please enter a number: 3 Factorial of 3 is: 6

Explanation
int num, fact = 1;
Here, in this program we have declared two int data type variables named num and fact.
// Asking for input printf("Please enter a number: "); scanf("%d", &num);
Then, the user is asked to enter a number. This number gets stored in the num named variable.
// Calculating Factorial for (int i = 1; i <= num; i++){ fact = fact * i; }
We initialize a for loop with i = 1, this loop keeps executing until i is greater than num.
Suppose, the user enter a number 3, then:
i <= num | fact * i = fact |
---|---|
1 <= 3 | 1 * 1 = 1 |
2 <= 3 | 1 * 2 = 2 |
3 <= 3 | 2 * 3 = 6 |
4 > 3 | loop terminates |
So, we get 3! = 6.
// Displaying output printf("Factorial of %d is: %d", num, fact);
And finally, the factorial of the entered number is displayed on the screen using printf() function.
C Program to Find Factorial of a Number Using While Loop
C Program
// C Program to Find Factorial of a Number Using While Loop #include <stdio.h> int main(){ int i = 1, num, fact = 1; // Asking for input printf("Enter a positive number: "); scanf("%d", &num); // Finding Factorial while (i <= num){ fact = i * fact; i++; } // Displaying output printf("Factorial of %d is: %d", num, fact); return 0; }
Output
Enter a positive number: 10 Factorial of 10 is: 3628800

Explanation
// Finding Factorial while (i <= num){ fact = i * fact; i++; }
In this program, we have used a while loop to calculate the factorial of the entered number.
C Program to Find Factorial of a Number Using Functions
C Program
// C Program to Find Factorial of a Number Using Functions #include <stdio.h> int fact(int num){ int i, factorial = 1; for (i = 1; i <= num; i++){ factorial = factorial * i; } return factorial; } int main(){ int num, f; // Asking for input printf("Please enter a number: "); scanf("%d", &num); // Calling out custom function f = fact(num); // Displaying output printf("Factorial of %d is: %d", num, f); return 0; }
Output
Please enter a number: 4 Factorial of 4 is: 24

Explanation
int fact(int num){ int i, factorial = 1; for (i = 1; i <= num; i++){ factorial = factorial * i; } return factorial; }
In this program, we have defined a custom function named fact which computes and returns the factorial of a number using a for loop.
C Program to Find Factorial of a Number Using Recursion
C Program
// C Program to Find Factorial of a Number Using Recursion #include <stdio.h> int recursive_fact(int num){ if (num == 0 || num == 1){ return 1; } else{ return (num * recursive_fact(num - 1)); } } int main(){ int num, factorial; // Asking for input printf("Please enter a number: "); scanf("%d", &num); // Calling out custom function factorial = recursive_fact(num); // Displaying output printf("Factorial of %d is: %d", num, factorial); return 0; }
Output
Please enter a number: 6 Factorial of 6 is: 720

Explanation
int recursive_fact(int num){ if (num == 0 || num == 1){ return 1; } else{ return (num * recursive_fact(num - 1)); } }
Here, we have defined a recursive function named recursive_fact which returns 1 if the argument passed is 0 or 1. Otherwise, it returns a recursive call again.
Conclusion
I hope after reading this post, you understand how to write a program to find the factorial of a number using C Programming language.
If you face any difficulty while understanding this program, then let us know in the comment section. We will be glad to solve your query.