Learn how to know the factorial of a positive integer with C.

By definition, a Factorial of a non-negative integer is the product of all the positive integers less than or equal to n as represented in the following math notation:

Math Factorial Number Notation

Factorials have a prominent place in mathematics as they are encountered in combinatorics, taylor expansions and in the number theory. For instance factorial of n is the number of ways one can arrange n different objects. If you are studying computer science, one of the most common tasks to solve in programming is how to obtain the factorial of a number.

In this article, we'll explain how you can obtain the factorial of a positive integer number in C with a very simple logic.

A. With iterations

The easiest way to do and understand the logic to obtain a factorial from a n number is with a for loop. You will need to define a for loop that will iterate from 1 up to the given n number. On every iteration the fact variable that initially has the 1 value will be updated with the result of the multiplication of itself with the index of the current iteration. In the following example we'll prompt for the number to calculate and we'll print the result at the end:

#include <stdio.h>

int main()
{
    // Note that initially, the fact variable is equals to 1
    int c, n, fact = 1;
    
    // Prompt user for the number to calculate, it can be statically defined as fact if you want.
    printf("Enter a number to calculate its factorial: \n");
    scanf("%d", &n);
    
    // Calculate factorial
    for (c = 1; c <= n; c++){
        fact = fact * c;
    }

    // Print result
    printf("Factorial of %d is: %d\n", n, fact);

    return 0;
}

You can convert it into a function if you want:

#include <stdio.h>

// declare method before using it to prevent error: conflicting types for 'factorial'
long factorial(int);

// Usage example:
int main()
{
    int fact = 10;

    // Prints: Factorial of 10 is: 3628800
    printf("Factorial of %d is: %d\n", fact, factorial(fact));

    return 0;
}

// Function that returns the factorial of a n number
long factorial(int n)
{
    int c;
    long result = 1;

    for (c = 1; c <= n; c++){
        result = result * c;
    }

    return result;
}

B. The recursive way

In programming, the recursion is a technique in which a function calls itself, for example, in the following code example, the factorial function will call itself:

#include <stdio.h>

// declare method before using it to prevent error: conflicting types for 'factorial'
long factorial(int);

// Usage example:
int main()
{
    int fact = 10;

    // Prints: Factorial of 10 is: 3628800
    printf("Factorial of %d is: %d\n", fact, factorial(fact));

    return 0;
}

// Function that returns the factorial of a n number
long factorial(int n)
{
    if (n == 0){
        return 1;
    }else{
        return(n * factorial(n-1));
    }
}

Note that the definition of the function is necessary in the recursion. What would be the preferred way to proceed for you?

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors