Factorial using recursion in c: To find factorial using recursion is a simple program to find factorial of a number.

Recursion in c :
Function calling itself is referred to 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)….

Algorithm to find factorial using recursion in c

  1. Declare the factorial function.
  2. Write the main function.
  3. Declare variables.
  4. Take an input number from the user.
  5. Pass number to the factorial function.
  6. Define factorial function (write recursive code to find factorial of a number)
  7. Print factorial

Program for factorial using recursion

#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 :

Factorial using recursion in c

Write A Comment