0% found this document useful (0 votes)
2 views2 pages

RECURSION FUNCTION

The best thing ever happened me is you are not going on call with Earth observation

Uploaded by

rajalakshmij2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

RECURSION FUNCTION

The best thing ever happened me is you are not going on call with Earth observation

Uploaded by

rajalakshmij2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

RECURSION FUNCTION:

 A function that calls itself is known as a recursive function.


And, this technique is known as recursion.
 But while using recursion, programmers need to be careful to
define an exit condition from the function, otherwise it will go
into an infinite loop.
 Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number,
generating Fibonacci series, etc.

1.8 Recursion

A function that calls itself is known as recursion. Recursion is the


process of calling the same function itself again and again until base
condition is satisfied.

Syntax:
Function1()
{
Function1();
}

How Recursion Function Works?

The recursion continues until some condition is met .i,e it must have at least one if statement to
terminate the recursion.

A Recursion Function must have the following statements:


- A statement to test whether function is calling itself again.
- A statement that calls the function itself must be an argument
- A conditional statement( if-else)
- A return statement
Program to find factorial of a given number:
#include<stdio.h> int fact(int n);
int main()
{
int f,n;
printf(" enter a number"); scanf("%d",&n);
f=fact(n);
printf("Factorial of a given number is %d",f); return(0);
}
int fact(int n)
{
if(n==1) return 1; else
return (n*fact(n-1));
}

Program to find fibonacci series of a given number:

#include <stdio.h>
int fibbonacci(int n);
int main()
{
int n,i,fibo;
printf(" enter the number:\n");
scanf("%d",&n);

printf("Fibbonacci of %d: " , n);


for(i = 0;i<n;i++)
{
fibo=fibbonacci(i);
printf("%d ",fibo);
}
}
int fibbonacci(int n) {
if(n == 0){
return 0;
} else if(n == 1) {
return 1;
} else {
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}

You might also like