C Programming Code Examples C > Mathematics Code Examples program computes factorial of a number program computes factorial of a number The factorial of a positive number n is given by: 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, 0! = 1 This program takes a positive integer from the user and computes factorial using for loop. #include <stdio.h> int main() { int n, j; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // show error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for(j=1; j<=n; ++j) { factorial *= j; // factorial = factorial*j; } printf("Factorial of %d = %llu", n, factorial); } return 0; }