## Factorial Program in C

We have explained what Factorial of a number in Mathematics is? We have shared that how to **find factorial of a number in PHP**. So if you want to know what is a factorial in mathematics? Check the post What Is A Factorial here.

Today we are going to share **factorial program in c**. As you know that the factorial of zero or 0 factorial is equal to 1 and 1 factorial is equal to 1 as well. So keep these in mind and the definition of factorial.

## What is factorial?

“Factorial of a number n is a product of all non-negative integers less than or equal to n written as n!” Negative numbers don’t have any factorial.

In mathematics you can write n! = 1 x 2 x 3 x 4….. x n.

Here is the **C Program to Find Factorial of a Number. **The program uses a for loop and returns a factorial. I have made the **C Function to Find Factorial of a Number **as simple as possible.

### Factorial program in C using a for loop:

#include <stdio.h> int main(){ int x, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&x); // As negative numbers are not allowed, error will be shown. if (x < 0){ printf("Negative numbers do not have factorial."); } else { for(i=1; i<=x; ++i) { factorial *= i; // factorial = factorial*i; } printf("Factorial of %d = %llu", x, factorial); } return 0; }

Output of **C Program to Find Factorial of a Number**:

Enter an integer: 4

Factorial of 4 = 24

The above function is using for loop to find a factorial, but now we are going to use the recursion method to find a factorial. Here is the **factorial program in C using recursion**:

### Factorial program in C using recursion

#include<stdio.h> long factorial(int); int main(){ int n; long f; printf("Please Enter a number\n"); scanf("%d", &n); if (n < 0){ printf("Negative numbers have no factorial.\n"); }else{ f = factorial(n); printf("%d! = %ld\n", n, f); } return 0; } long factorial(int n){ if (n == 0){ return 1; }else{ return(n * factorial(n-1)); } }