Factorial Program in C

Last Updated : 5 Sep, 2026

Given a number N, the task is to find the factorial of it. A factorial is the product of all the natural numbers less than or equal to the given number N.

Examples

Input: N = 5
Output: 120
Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120

Input: N = 0
Output: 1
Explanation: 0! = 1 by definition

Methods to Find Factorial in C

The factorial of a number can be calculated using iteration and recursion. For very large factorials, a character array can be used to store the result beyond the range of standard integer data types.

Find Factorial Using a Loop

The iterative approach uses a loop to multiply all numbers from 1 to N.

Steps

  • Initialize fact to 1.
  • Run a loop from 1 to N.
  • Multiply fact by the current number.
  • Return the value stored in fact.
C
#include <stdio.h>

unsigned int factorial(unsigned int N) {
    int fact = 1, i;

    // Loop from 1 to N to get the factorial
    for (i = 1; i <= N; i++) {
        fact *= i;
    }

    return fact;
}

int main() {
    int N = 5;
    int fact = factorial(N);
    printf("Factorial of %d is %d", N, fact);
    return 0;
}

Output
Factorial of 5 is 120

Explanation: fact is initialized to 1, and the loop multiplies it by every number from 1 to n and the function then returns the calculated factorial.

Find Factorial Using Recursion

The recursive approach uses the below relation. The recursion stops when N becomes 0, since 0! = 1.

N! = N × (N - 1)!

Steps

  • Check whether N is 0.
  • Return 1 if the base case is reached.
  • Otherwise, call the function with N - 1.
  • Multiply the current value of N with the returned result.
C
#include <stdio.h>

unsigned int factorial(unsigned int n) {
  
    // Base Case:
    if (n == 1) {
        return 1;
    }

    // Multiplying the current N with the previous product
    // of Ns
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

Output
Factorial of 5 is 120

Explanation: The function recursively calls itself with n - 1 until n becomes 0, which is the base case and the returned values are multiplied together to calculate the factorial.

Find Factorial for Large Numbers

The factorial grows rapidly and can exceed the range of standard integer data types. To handle such values, the result can be stored as a sequence of digits in a character array.

Steps

  • Initialize the factorial as "1".
  • Start multiplying from 2 to N.
  • Multiply each digit of the current result by the current number.
  • Handle the carry generated during multiplication.
  • Store the resulting digits in the character array.
  • Print the final factorial.
C
#include <stdio.h>
#include <string.h>

// Function to multiply a string (representing a number)
// by an integer
void multiplyString(char num[], int factor) {
    int len = strlen(num);
    int carry = 0;

    for (int i = len - 1; i >= 0; i--) {
        int digit = num[i] - '0';
        int product = digit * factor + carry;
        num[i] = (product % 10) + '0';
        carry = product / 10;
    }

    // Handling the carry by adding digits to the front
  	// of the number
    while (carry) {
        for (int i = strlen(num); i >= 0; i--) {
            num[i + 1] = num[i];
        }
        num[0] = (carry % 10) + '0';
        carry /= 10;
    }
}

// Function to find factorial using a string
void factorialString(int N) {
  
    // Use a large enough buffer
    char fact[1000];
  
    // Initialize result as "1"
    strcpy(fact, "1");

    for (int i = 2; i <= N; i++) {
      
        // Multiply the string in each iteration
        multiplyString(fact, i);
    }

    printf("Factorial of %d is %s", N, fact);
}

int main() {
    int N = 5;
    factorialString(N);
    return 0;
}

Output
Factorial of 5 is 120

Explanation

  • fact stores the factorial as a character array to handle large numbers.
  • multiplyString() multiplies the current result by each number and handles the carry.
  • The loop calculates the factorial by multiplying fact with every number from 2 to N.
  • The final factorial is printed as a string.
Comment