Recursion:
Recursion is the process by which a
function calls itself repeatedly.
Program to illustrate factorial of a
given number using recursion :
fact(n) = 1
n=0 // Base case
= n * fact ( n -1) n>0
// Recursive case
// Recursion program on factorial we
can have the value upto n=15
#include<stdio.h>
int fact; // global declaration
int factorial(int); // function
declaration
void main()
{
int n ;
printf("enter any value:\n");
scanf("%d",&n);
fact=factorial(n); // calling
function
printf("factorial of %d =
%d",n,fact);
}
int factorial(int n) // called
function
{
if(n==0)
return 1;
else
fact=n*factorial(n-1);
return (fact);
}
output:
No comments:
Post a Comment