C Program to Find Factorial of a Number

Created with Sketch.

Factorials in C: A Comprehensive Guide


Factorials are fundamental mathematical operations often used in various mathematical and computational scenarios. In this blog post, we’ll delve into the world of factorials, exploring what they are, their significance, and how to calculate them using C programming. We’ll provide a detailed C program to find the factorial of a given number, accompanied by an explanation of the underlying algorithm.

Understanding Factorials


In mathematics, the factorial of a non-negative integer is the product of all positive integers less than or equal to . It is denoted by ! and is defined as:

By convention, 0! is defined to be 1.


Factorials find applications in various mathematical and combinatorial problems. They are used to calculate permutations, combinations, and in the expansion of binomial coefficients. Additionally, factorials play a role in the analysis of algorithms and are a common topic in introductory programming exercises.

The Factorial Algorithm


To calculate the factorial of a number , we can use a simple iterative approach. The algorithm involves initializing a variable to store the result and then iteratively multiplying it by numbers from 1 to .

  1. Input: Get an integer from the user.
  2. Initialization: Set to 1.
  3. For Loop: Iterate from 1 to .
    • For each iteration, multiply by the current number.
  4. Output: Print the calculated factorial.

C Code Example

#include <stdio.h>

// Function to calculate the factorial of a number
unsigned long long calculateFactorial(int n) {
    // Variable to store the factorial, initialized to 1
    unsigned long long factorial = 1;

    // Calculate factorial using a loop
    for (int i = 1; i <= n; ++i) {
        factorial *= i;

    return factorial;

int main() {
    int num;

    // Input: Get the number for which factorial is to be calculated
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);

    // Check for negative input
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        // Calculate and print the factorial
        printf("Factorial of %d = %llu\n", num, calculateFactorial(num));

    return 0;

Output Example


Enter a non-negative integer: 5
Factorial of 5 = 120


Understanding factorials is essential for both mathematical and programming contexts. The ability to calculate factorials is a basic skill in programming and algorithmic problem-solving. The provided C code offers a clear and concise implementation of the factorial calculation algorithm, making it accessible for learners and programmers at various levels.

As you explore factorials and their applications, you’ll gain insights into the iterative nature of many mathematical operations. The factorial algorithm presented here serves as a foundational example, and mastering it will pave the way for tackling more complex mathematical challenges and programming tasks.

Factorials are not just mathematical entities; they are gateways to a deeper understanding of algorithms, recursion, and problem-solving. Whether you are a student delving into mathematical concepts or a programmer aiming to strengthen your coding skills, grasping the intricacies of factorials is a valuable journey in your computational exploration.

Leave a Reply

Your email address will not be published. Required fields are marked *