✓ Link copied to clipboard
C 📅 July 4, 2026 ⏱ 5 min read

C Program to Find Factorial of a Number

C Program to Find Factorial of a Number

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 while loop.
  • The stdio.h library 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:

factorial.cC▶ Run
#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.

▶ Open Compiler

Output of the Factorial Program

When you run this code, you can expect an output like this:

Outputtext
Enter a number: 5
Factorial of 5 = 120
Output of C Program to Find Factorial of a Number
Output of C Program to Find Factorial of a Number

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:

  1. Read the integer n from the user.
  2. Set fact = 1 as the initial factorial value.
  3. Repeat while n > 1:
  4. Multiply fact by n.
  5. Decrement n by 1.
  6. 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])
Flowchart: the algorithm step by step

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 variables n for input and fact initialized 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 until n is greater than 1.
  • fact *= n;: Multiplies fact by n to accumulate the factorial product.
  • n--;: Decrements n for 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 answer

What is the factorial of 5?

🎉Correct! Correct! 5 factorial is calculated as 5*4*3*2*1 = 120.
🤔Not quite. Try using 5! = 5*4*3*2*1.

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:

A factorial is the product of all positive integers up to a certain number, shown as n!. Like, 5! equals 5 * 4 * 3 * 2 * 1 = 120.
Make sure to initialize fact to 1 before the loop. If you don’t, multiplication will mess up your results.
Sure, you can use recursion to find factorials. But the iterative method is often better since it has less function call overhead and stack usage.
The maximum depends on the data type for fact. With int, it usually maxes out around 12!. For bigger numbers, think about using long or unsigned long long.
The program doesn’t handle negative numbers well. Factorials aren’t defined for them, so adding a check to return an error for those inputs is key.

If you found this program helpful, check out these related programs:

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.