0% found this document useful (0 votes)
1 views8 pages

Recursion

This document contains the concept of Recursion with programs. It will help the students of class XII, ISC

Uploaded by

Malar
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)
1 views8 pages

Recursion

This document contains the concept of Recursion with programs. It will help the students of class XII, ISC

Uploaded by

Malar
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

Recursion

Recursion is a programming technique where a function calls itself


again and again.
Base case: Every recursive function uses a condition to terminate.
It is known as base case. As soon as the condition for base case is
true the control terminates the function otherwise, it keeps calling
the function again and again(infinitely).
The statement that calls the function repeatedly from its block is
said to be ‘Recursive case’.

Advantages of recursion
* It makes the code small and clear so the program code becomes
clear and easy to understand.
* Minimum number of variables are used
* The algorithm is precise, readable and efficient
*Easier to debug and obtain error free result.

Limitations of recursion
* More execution time
* It can result in stack overflow
* Occupies more space
Example code of a recursive function:

Types of recursion
1. Direct recursion (linear recursion)
In this type of recursion the function is called from within the same
block.
Two categories of direct recursion are
* Tail recursion: In this type of recursion recursive call is the last
statement of the function. For example factorial of a number code.

//Recursive factorial function

int fact(int n)

if(n==0) return 1;

else

return (n* fact(n-1));

}
* Binary recursion:In this type of recursion function calls itself
from two distinct points. For example GCD program code.

//Recursive GCD function

int gcd(int n1,int n2)

if(n1==n2)

return n1;

else

if(n1>n2)

return(gcd(n1-n2,n2));

else

return(gcd(n1,n2-n1));

}
2. Indirect Recursion (Mutual Recursion):
In this type of recursion the function is called from another function
block mutually. Let two functions A and B such that function B is
called from function A and vice versa then this technique is called
mutual recursion.

Write a program to find the given number is Armstrong number or


not with the help of a recursive function.

Armstrong number is a number that is equal to the sum of digits


raised to the power as length of the number.

import [Link].*;

public class armnum

static double n;int l;

void armnum(int nn)

n=nn;

String str=[Link](n);

l=[Link]();

double sum_pow(double i)

{
if(i==0)

return 0;

else

double d=i%10;

double sum=[Link](d,l);

return(sum+sum_pow(i/10));

void isarmstrong()

if(sum_pow(n)==n)

[Link]("armstrong number");

else

[Link]("not armstrong number");

public static void main()throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the number");

double n=[Link]([Link]());
armnum obj=new armnum();

[Link]();

Write a program to search an element from the array using binary


search technique.

import [Link].*;

public class binary

int a[];

int u,l,n,m;

binary(int nn)

n=nn;

a=new int[n];

for(int i=0;i<n;i++)

a[i]=0;

l=0;

u=n-1;
}

void readdata()throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the elements");

for(int i=0;i<n;i++)

a[i]=[Link]([Link]());

int binary_search(int v)

m=-1;

while(u<=n-1)

m=(l+u)/2;

if(a[m]>v)

u=m-1;
m=binary_search(v);

else if(a[m]<v)

l=m+1;

m=binary_search(v);

else

break;

return m;

void display()

if(m==-1)

[Link]("the element is absent");

else

[Link]("the element is on "+m+"


index");

public static void main()throws IOException


{

binary obj=new binary(6);

[Link]();

obj.binary_search(4);

[Link]();

You might also like