Factorial Of N Number In C

In this article, I will explain, how to find the factorial number in C language. There are 2 ways to find the factorial number

  1. Using loop
  2. Using Recursion function

here I’m using the 1st way.

What is a Factorial Number?

The factorial number is the multiplication of all numbers smaller than that number.

For example,

3! = 3*2*1 = 6

Code

#include <stdio.h>

int main() {
    int n, i;
    int f = 1;
    
    printf("Enter an integer: ");
    scanf("%d", &n);
    
    if (n < 0)
        printf("Factorial of a negative number doesn't exist...");
    else {
        for (i = 1; i <= n; ++i) {
            f *= i;
        }
        printf("Factorial of %d = %d", n, f);
    }

    return 0;
}

Output

Enter an integer: 10
Factorial of 10 = 3628800

Submit a Comment

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

Subscribe

Select Categories