**Factorial using recursion in c** is a program for factorial using recursion.

Find factorial using recursion is a simple program to find factorial of a number.

In this tutorial we are going to learn the factorial of a number, find factorial using recursion, and program for factorial using recursion.

**Recursion in c :**

Function calling itself is referred as recursion.

**What is factorial :**

The factorial is the product of all numbers less than or equal to N but greater than or equal to 1.

The factorial value of 0 is defined as equal to 1.

**Factorial formula :**

**N!** => N*(N-1)*(N-2)….

**How to find factorial of a number ?**

To find factorial of a number first we take number from user. after that using factorial formula we find factorial of a number. print factorial.

**Algorithm to find factorial using recursion :**

- Start
- Declare factorial function.
- Write main function.
- Declare variables.
- Take a input number from user.
- Pass number to factorial function.
- Define factorial function (write recursive code to find factorial of a number)
- Print factorial
- End.

## Program for factorial using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<stdio.h> long factorial(int); int main() { int n; long f; printf("Enter an integer to find its factorial\n"); scanf("%d", &n); if (n < 0) printf("Factorial of negative integers isn't defined.\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)); } |

**Output :**

**Explanation :**

1. First we declare the header files.

2. Next we declare the recursion function .

long factorial(int);

3. write the main function.

4. Declare the variables.

f => to store factorial

n => to store number

5. Take a number input from user to find factorial.

printf(“Enter an integer to find its factorial\n”);

scanf(“%d”, &n);

=> n= 3

8. we check whether number is negative if negative number then we cant find factorial.

if ( n < 0) this condition becomes false and else executed.

In else block we pass number to factorial function.

f = factorial(n); => factorial(3)

and function executed will number 3

**At call 1 :**

long factorial(int n) => n = 3

if (n == 0) => 3 == 0 this condition false else executed.

return(n * factorial(n-1)) => 3 * factorial(2) => 2

**At call 2 :**

long factorial(int n) => n = 2

if (n == 0) => 2 == 0 this condition false else executed.

return(n * factorial(n-1)) => 2 * factorial(1) => 3

**At call 3 :**

long factorial(int n) => n = 1

if (n == 0) => 1 == 0 this condition false else executed.

return(n * factorial(n-1)) => 1* factorial(0) => 6

**At call 4 :**

long factorial(int n) => n = 0 stop recursive call.

return to main and print factorial of a number.

printf(“%d! = %ld\n”, n, f);

=> 3! = 6

**Recommended posts**

Fibonacci series suing recursion

## Leave a Reply