0
0
CProgramBeginner · 2 min read

C Program to Find Factorial of a Number

A C program to find factorial of a number uses a loop to multiply all integers from 1 to that number, like for(int i=1; i<=n; i++) factorial *= i;.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial of a number, think of multiplying all whole numbers from 1 up to that number. Start with 1, then multiply by 2, then 3, and so on until you reach the number. This gives the total product called factorial.
📐

Algorithm

1
Get input number from the user
2
Initialize a variable factorial to 1
3
Loop from 1 to the input number
4
Multiply factorial by the current loop number each time
5
After the loop ends, print the factorial value
💻

Code

c
#include <stdio.h>

int main() {
    int n, factorial = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }
    printf("Factorial of %d is %d\n", n, factorial);
    return 0;
}
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Input

User enters 5, so n = 5

2

Initialize factorial

factorial = 1

3

Loop start

i = 1, factorial = 1 * 1 = 1

4

Loop iteration 2

i = 2, factorial = 1 * 2 = 2

5

Loop iteration 3

i = 3, factorial = 2 * 3 = 6

6

Loop iteration 4

i = 4, factorial = 6 * 4 = 24

7

Loop iteration 5

i = 5, factorial = 24 * 5 = 120

8

Loop ends

Loop ends as i > n

9

Output

Print 'Factorial of 5 is 120'

ifactorial
11
22
36
424
5120
💡

Why This Works

Step 1: Initialize factorial

We start with factorial = 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply in loop

Each loop multiplies factorial by the current number i, building the product step by step.

Step 3: Final result

After the loop finishes, factorial holds the product of all numbers from 1 to n, which is the factorial.

🔄

Alternative Approaches

Recursive function
c
#include <stdio.h>

int factorial(int n) {
    if (n <= 1) return 1;
    else return n * factorial(n - 1);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial of %d is %d\n", n, factorial(n));
    return 0;
}
Uses function calling itself; elegant but uses more memory due to call stack.
Using while loop
c
#include <stdio.h>

int main() {
    int n, factorial = 1, i = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (i <= n) {
        factorial *= i;
        i++;
    }
    printf("Factorial of %d is %d\n", n, factorial);
    return 0;
}
Uses while loop instead of for; same logic but different loop style.

Complexity: O(n) time, O(1) space

Time Complexity

The program runs a single loop from 1 to n, so it takes linear time proportional to n.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

Both iterative and recursive methods have O(n) time, but iterative uses less memory and is generally faster.

ApproachTimeSpaceBest For
Iterative for loopO(n)O(1)Simple and efficient for all inputs
Recursive functionO(n)O(n)Clear logic but uses more memory
While loopO(n)O(1)Same as for loop, just different syntax
💡
Always initialize factorial to 1, not 0, because multiplying by 0 gives zero.
⚠️
Beginners often forget to initialize factorial to 1 or start loop from 0, causing wrong results.