C Program to Find Factorial of a Number

The factorial of a positive number n is given by:

  1. factorial of n (n!) = 1 * 2 * 3 * 4....n

The factorial of a negative number doesn’t exist. And, the factorial of 0 is 1.

Factorial of a Number

  1.   #include <stdio.h>
  2.   int main() {
  3.   int n, i;
  4.   unsigned long long fact = 1;
  5.   printf("Enter an integer: ");
  6.   scanf("%d", &n);
  7.   // shows error if the user enters a negative integer
  8.   if (n < 0)
  9.   printf("Error! Factorial of a negative number doesn't exist.");
  10.   else {
  11.   for (i = 1; i <= n; ++i) {
  12.   fact *= i;
  13.   }
  14.   printf("Factorial of %d = %llu", n, fact);
  15.   }
  16.   return 0;
  17.   }