In: Computer Science
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
ANSWER:
Code with explanation:
// Function to find factorial of a number n :
int find_factorial(int n)
{
//Factorial of 0 is 1
if(n==0)
return(1);
//Function calling itself: recursion
//Here we call the function recursively for a number less than the number in the previous recursive call
return(n*find_factorial(n-1));
}
//The code below is to call the above Factorial function and pass a value entered by the user, to check the output
#include<stdio.h>
int main()
{
int num;
//Ask user for the input and store it in num
printf("\nEnter any integer number:");
scanf("%d",&num);
//Calling the factorial function here
int fact = find_factorial(num);
//Printing the factorial
printf("\nfactorial of %d is: %d",num, fact);
}
Code: (Screenshot of executed code)
Code output:
Final clean function to copy:
// Function to find factorial of a number n :
int find_factorial(int n)
{
//Factorial of 0 is 1
if(n==0)
return(1);
//Function calling itself: recursion
return(n*find_factorial(n-1));
}
So, this was the full Python code for the given question, with full explanation and execution and screenshots. Hope it helps. If it did, plz consider upvoting , it would mean a lot to me. Plz feel free to ask and clarify any doubts, THANKS and have a Great day :)