How to find the factorial of a number in C?

#include <stdio.h>

int main(void) {
    int n = 5;
    unsigned long long fact = 1;

    for (int i = 2; i <= n; i++) {
        fact *= i;
    }
    printf("%d! = %llu\n", n, fact);
    return 0;
}

Output:

Output of the above program is

5! = 120

Factorial grows very fast. Even unsigned long long overflows after 20!. The recursive way is in C Recursion.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.