What is recursive function ? Write a C program using recursion for calculating factorial of an integer
Answer:
The
process in which a function in turn calls itself is
called
recursion. And the function is called recursive function.
When
a called function in turn calls another function again and again the
process
of this calling is called recursion.
An
example is given below:
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n,f;
clrscr();
printf ("Input the value of n:
");
scanf ("%d", &n);
f=fact (n);
printf ("Factorial: %d",
f);
getch();
}
int fact(int n)
{
int p;
if(n==1)
return 1;
else
p=n*fact(n-1);
return p;
}
Simple Output:
Input the value of n: 5
Factorial: 120