Factorial of a number using recursion

Factorial Number

Factorial says to multiply all whole numbers from the chosen number down to 1.

The symbol is “!”

Examples:

5! =5 x 4 × 3 × 2 × 1 = 120
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040

Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to 1, then

n ! = n ( n – 1)( n – 2)( n – 3) … (3)(2)(1)

The factorial values for negative integers are not defined.

 

factorial number1

Implementation of factorial of number

First of all we want to solve this problem using recursion,recursion means Recursion is a process in which a function calls itself as a subroutine. This allows the function to be repeated several times, since it calls itself during its execution.

initialize n and f variable and fact is a function the function is call itself by repetition . if n equal 0 then return 1 else return (n*fact(n-1)).

Code of factorial of number using recursion

  #include<stdio.h>
  int fact(int);
  void main()
{
  int n,f;
  printf("enter number for finding factorial\n");
  scanf("%d",&n);
  f=fact(n);
  printf("factorial is%d",f);

}
  int fact(int n)
{
  if(n==0)
     return 1;
  else
    return(n*fact(n-1));

}

Output

enter number for finding factorial

4

factorial is 24