Home Uncategorized factorial using recursion in C

factorial using recursion in C

SHARE

Write a C Program to find Factorial of a given number using functions with Recursion.

#include<stdio.h>
long int fact(int);
void main()
{
   int n; //For read user value.
   long int r; //to Store the result value.
   printf(“Enter a number : “);
   scanf(“%d”,&n);
   if(n<=0) //Checking entered value is valid or not
   {
      printf(“Please enter a valid number\n”);
      return;
   }
   r=fact(n); //Function calling and store the resultent value
   printf(“Factorial of given number is : %ld\n”,r);
   return;
}
long int fact(int a)
{
   if(a==1)
      return 1;
   return a*fact(a-1); //Recurssion calling of the function
}
Output:
Enter a number : 9
Factorial of given number is : 362880

Factorial of a given number using functions without Recursion.

Back to the programs list.