# C Program to Find Factorial of a Number

C Program to Find Factorial of a Number:

The factorial of a positive integer n is equal to 1*2*3*…n. You will learn to calculate the factorial of a number using for loop in this example.

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`

## Example: Factorial of a Number

```#include <stdio.h>
int main()
{
int n, i;
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(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

```

Output

```Enter an integer: 10
Factorial of 10 = 3628800
```

This program takes a positive integer from the user and computes factorial using for loop.

Since the factorial of a number may be very large, the type of factorial variable is declared as `unsigned long long`.

If the user enters negative number, the program displays error message.

C Program to Find Factorial of a Number Using Recursion:

Example to find factorial of a non-negative integer (entered by the user) using recursion.

```#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
```

Output

```Enter a positive integer: 6
Factorial of 6 = 720```