c program to find factorial using recursion
C Programming

C Program to Find Factorial using Recursion

C Program to Find Factorial using Recursion

In this tutorial we have explained the C Program to find the factorial using recursion.

C Program to calculate the Factorial of a non negative number using recursive approach is discussed here in this tutorial.

Algorithm

Step 1– Declare function fact() that take the number as parameter.

Step 2– Accept the input number from the user

Step 3- Declare a variable f to store the result

Step 4 – Call the fact() inside main () function and assign it to variable f to store the result return by the function fact().

Step 5 – Define the body of function fact()

Step 6 – If number entered by is zero then function will return 1 else function will recursively call to it self.

Step 7 – Print the value of f to display the factorial.

C Program to Calculate the Factorial of a number using recursion is given below

#include<stdio.h>

int fact(int n);

int main()
{
int number;
int f;
printf(“Enter a number “);
scanf(“%d”, &number);
f = fact(number);
printf(“Factorial of %d is %d\n”, number, f);
return 0;
}

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

Output

c program to find factorial using recursion

When we execute the above program and enter the value of n as 5 then at first fact() function will be called for 5 second time it will call for 4 in this way fact() will be call upto 0 and finally  we get the factorial 120.

 

Leave a Reply

Your email address will not be published. Required fields are marked *