RECURSION
Recursion is the process of repeating items in a self-similar way. If a program allows you to call a function inside the same function, then it is called a recursive call of the function.
The C programming language supports recursion, i.e., a function to call itself. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating the Fibonacci series, etc.
1. FACTORIAL OF A NUMBER
#include<stdio.h>
int find_factorial(int);
int num, fact;
printf("Enter any integer number:")
scanf("%d", &num);
fact=find_factorial(num);
printf("Factorial of %d is : %", num , fact);
return 0;
}
int find_factorial(int n)
{
if(n==0)
return (1);
return (n*find_factorial(n-1));
}
int find_factorial(int);
int num, fact;
printf("Enter any integer number:")
scanf("%d", &num);
fact=find_factorial(num);
printf("Factorial of %d is : %", num , fact);
return 0;
}
int find_factorial(int n)
{
if(n==0)
return (1);
return (n*find_factorial(n-1));
}
OUTPUT:-
Enter any integer number: 4
Factorial of 4 is: 24
1. FIBONACCI SERIES
#include<stdio.h>
int Fibonacci (int i)
{
if(i==0)
{
return 0;
}
if(i==1)
{
return 1;
}
return fibonacci(i-1) + fibonacci (i-2);
}
int main()
{
int i;
for (i=0; I<9; i++)
{
printf("%d", fibonacci(i));
}
return 0;
}
int Fibonacci (int i)
{
if(i==0)
{
return 0;
}
if(i==1)
{
return 1;
}
return fibonacci(i-1) + fibonacci (i-2);
}
int main()
{
int i;
for (i=0; I<9; i++)
{
printf("%d", fibonacci(i));
}
return 0;
}
OUTPUT:-
0
1
1
2
3
5
8
13
21
0 Comments
Doubts? Please let our team know So that we can serve you better.