Factorials show up all over the place. Like when you're calculating permutations or combinations. For example, with a number like 5, the factorial is the product of all positive integers up to that point. So, 5! equals 5 * 4 * 3 * 2 * 1 = 120. Let’s whip up a C program to find the factorial of a number using a simple loop.
Prerequisites for the Factorial Program in C
Here's what you need to know before writing this program:
- Basic understanding of C syntax and structure.
- Using a loop, specifically a
whileloop. - The
stdio.hlibrary for input/output functions. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Find Factorial of a Number
Here's the complete code for calculating the factorial:
#include <stdio.h>
int main() {
int n, fact = 1;
// Input from user
printf("Enter a number: ");
scanf("%d", &n);
// Calculate factorial using a while loop
while (n > 1) {
fact *= n;
n--;
}
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Factorial Program
When you run this code, you can expect an output like this:
Enter a number: 5
Factorial of 5 = 120

Algorithm to Find Factorial in C
Finding a factorial involves multiplying the integer sequentially down to 1. Here is the step-by-step process the program follows:
- Read the integer
nfrom the user. - Set
fact = 1as the initial factorial value. - Repeat while
n > 1: - Multiply
factbyn. - Decrement
nby 1. - Output the factorial result.
flowchart TD
A([Start]) --> B[/Read n/]
B --> C["fact = 1"]
C --> D{"n > 1?"}
D -->|Yes| E["fact = fact * n"]
E --> F["n = n - 1"]
F --> D
D -->|No| G([End])How the C Program to Find Factorial Works
Let's break down each line of the code:
#include <stdio.h>: Includes the standard input/output library.int main(): The main function where the program execution begins.int n, fact = 1;: Declares integer variablesnfor input andfactinitialized to 1 to hold the factorial result.printf("Enter a number: ");: Prompts the user to enter a number.scanf("%d", &n);: Reads the integer value from user input.while (n > 1): A loop that continues untilnis greater than 1.fact *= n;: Multipliesfactbynto accumulate the factorial product.n--;: Decrementsnfor the next loop iteration.printf("Factorial of %d = %d\n", n, fact);: Outputs the factorial result to the console.return 0;: Ends the program successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the factorial of 5?
Time and Space Complexity of the C Factorial Program
Let's analyze the complexity of this factorial program:
Time complexity: O(n). Here, n is the number you input. For example, if you input 5, the loop runs 4 times (while (n > 1)). Each loop iteration does a multiplication and a decrement operation. If n is 10, it means 9 iterations.
Space complexity: O(1). The program uses only a fixed number of variables, specifically n and fact, both of which are integers and don't grow with the input size. This makes the space usage constant.
Common mistake: Not initializing fact to 1 will lead to incorrect results because you might multiply with garbage values.
Key takeaway: Factorials grow super fast and can quickly exceed standard integer limits, so keep in mind to use the right data types for bigger numbers.
FAQs About C Program to Find Factorial of a Number
Here are some common questions students ask when writing this program:
n!. Like, 5! equals 5 * 4 * 3 * 2 * 1 = 120.fact to 1 before the loop. If you don’t, multiplication will mess up your results.fact. With int, it usually maxes out around 12!. For bigger numbers, think about using long or unsigned long long.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Display Fibonacci Sequence with Example
- C Program to Add Two Integers with Explanation and Output
- C Program to Multiply Two Floating-Point Numbers
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
Conclusion
Factorial calculations pop up everywhere, from math studies to real-world uses, like calculating permutations. Experiment with different input sizes to see how quickly results balloon. Wanna dive deeper? Next, tackle prime number detection.
Put your C skills to the test in our C programming quiz and try out more code in the C online compiler.