C program to find factorial using function and recursion

C program to find factorial using function  and recursion :  

 In this program, we will read a number and then find (calculate) of factorial of that number using recursion.



What is factorial: Factorial of a number is the product of that number with their all below integers.
For example (Factorial of 5) is: !5 = 5*4*3*2*1 = 120

!5  thats means    5* !5-1 
or !5 = 5*!4


Example : 

   Input   : Enter the number 5 

  Output : factorial = 120 



Code : 

#include<stdio.h>
 long int  fact(int number)
{
    if(number==0)
    return 1;
    return number*fact(number-1);
}

int main()
{
     int n;
    printf("Enter the factoril number : ");
    scanf("%d",&n);
      long int  bb=fact(n);
    printf(" factorial =  %ld",bb);
    return 0;
}



output : factorial = 120







No comments

Powered by Blogger.