Write a recursive function in C programming to find factorial of any given number. How to find factorial of any number using recursion in C programming. Logic to find factorial of a number using recursion in C programming.

**Example**

**Input**

Input any number: 5

**Output**

Factorial of 5 = 120

## Required knowledge

Basic C programming, Function, Recursion

Must know - Program to find factorial of a number using loop

## Declare recursive function to find factorial of a number

- First let us give a meaningful name to our function, say fact().
- The factorial function accepts an integer input whose factorial is to be calculated. Hence the function declaration should look like fact(int num);.
- The function returns the calculated factorial as an integer value. Since, factorial can grow rapidly hence the suitable return type for this function is long long.

Recursive function declaration to find factorial of a number is - long long fact(int num);

## Logic to find factorial of a number using recursion

The above mathematical function defines a recursive approach to find factorial of any number.

Factorial of 0 is 1 which we will use as a base condition i.e. if(num == 0) return 1;. If the number is positive then make a recursive function call to calculate factorial i.e. num * fact(num - 1);

## Program to find factorial using recursion

```
/**
* C program to find factorial of any number using recursion
*/
#include <stdio.h>
/* Function declaration */
long long fact(int num);
int main()
{
int num;
long long factorial;
/* Input an integer from user */
printf("Enter any number: ");
scanf("%d", &num);
factorial = fact(num); // Call factorial function
printf("Factorial of %d is %lld", num, factorial);
return 0;
}
/**
* Function to compute and return factorial of any number recursively.
*/
long long fact(int num)
{
// Base condition
if(num == 0)
return 1;
else
return num * fact(num - 1);
}
```

**Note:** Some compilers doesn't supports long long type so you must replace it with long type and the format specifier with %ld, otherwise compiler will report errors.

Output

Enter any number: 10 Factorial of 10 is 3628800

Happy coding 😉

### Recommended posts

- Function and recursion programming exercise index.
- C program to find GCD of a number using recursion.
- C program to LCM of a number using recursion.
- C program to print array elements using recursion.
- C program to find sum of array elements using recursion.
- C program to find maximum and minimum in array using recursion.

<pre><code> ----Your Source Code---- </code></pre>