Professional Documents
Culture Documents
Recursion Vs Iteration:
i) Iteration is typically a faster process and uses less memory.
ii) Recursion is comparatively a slow process and uses large memory.
Recursive Method:
It is a programming technique whereby a procedure calls itself repeatedly until some
specific condition has been satisfied. Recursion is sometimes called circular definition.
The call to the method appears in the method‟s body itself.
Types of Recursion:
1) By Recursive Technique:-
i) Direct
ii) Indirect
i) Base criteria: or base case is the case for which the function does not call itself.
This involves certain pre known value (called base value) or condition before
beginning a task that cause recursion to end. For e.g. Any number to the power 0 is 1
(Pre-known case)
ii) Recursive criteria: is the repetitive condition of a recursive method such that, each
time the function calls itself, the argument of the function must be closer to a base
value.
Mechanics of Recursion
i) Recursion works on the principle of a push–down stack.
ii) The memory space needed for each function call is placed on the stack, and then taken
off again when the function completes its execution. Thus, the last memory opened will be
the first memory to be closed which means the last function call returns to the previous
function call and so on.
Advantage:
Recursive code is typically much shorter than its iterative version. It makes the code compact.
Disadvantage:
i) Recursion tends to slow down execution of programs as it consumes extra storage
space. Each time a recursive call is made, the stack grows as a new memory space is
allocated to each local variable of the recursive routine. The computer may run out of
memory, if the recursive calls are not checked.
ii) Faulty programs are very difficult to debug.
B) Factor Operations
E.g.1: To find the sum of factors of a number excluding itself
int sumfactor(int n, int i) { // i = 1 initially
if (i = = n) return 0 ; // base case
if (n % i == 0) return (i + sumfactor(n, i+1));
return (sumfactor(n, i+1)) ; // recursive case
}
E.g.2: To count the factors of a number including itself
int countfactor(int n, int i) { // i = 1 initially
if (i > n) return 0 ; // base case
if (n % i == 0) return (1+ countfactor(n, i+1));
return (countfactor(n, i+1)) ; // recursive case
}
E.g.3: To check a prime number
boolean isprime(int n, int i) { // i = 2 initially
if (n = = i) return true ; // base case
if (n = = 1 || n % i == 0) return false; // base case
return (isprime(n, i+1)) ; // recursive case
CITY MONTESSORI SCHOOL
SECTOR-D LDA KANPUR ROAD, LUCKNOW
PHONE: 0522 2435690 FAX: 0522 2435690
}
D) Series Problems
E.g.1: To print the first n terms of fibonacci series like 0, 1, 1, 2…
void fibser(int a,int b,int n) { // a=0, b=1 initially
if (n = = 0) return ; // base case
System.out.print(a+" ");
fibser(b, a+b, n–1); // recursive case
}
E.g.2: To print the sequence 1, 3, 6, 11, 20, n terms
void printser1(int n) {
if (n < 0) return ; // base case
printser1(n – 1 ); // recursive case
CITY MONTESSORI SCHOOL
SECTOR-D LDA KANPUR ROAD, LUCKNOW
PHONE: 0522 2435690 FAX: 0522 2435690
int a=(int) Math.pow(2,n) + n;
System.out.print(a+" ");
}
E.g.3: To print the sequence 1, 11, 111, 1111….. n terms
void printser3(int n, int a) { // a=1 initially
if(n > 0) { // recursive case
System.out.print(a+" ");
printser3(n-1,a*10+1);
}
}
E.g.4: To find the sum of the series 1 – 3 + 5 – 7 + ....+n terms
int sumser1(int n) {
if(n<1) return 0; // base case
int t=(2*n-1) * (int) Math.pow(–1 ,n–1); // recursive case
return (t + sumser1(n–1));
}
E.g.5: To return the sum of the series 1 + x2 + x3 +....+xn
int sumser2(int x,int n) {
if (n<0) return 0; // base case
int t = (int) Math.pow(x, n);
return (t+ sumser2(x, n–1)); // recursive case
}
E.g.6: To find the sum of the series 1 + 3 + 7 + 15 + 31....+n terms
int sumser3(int n) {
if (n<1) return 0; // base case
return ((int) Math.pow(2, n) –1 +sumser3(n–1)); // recursive case
}
int fib(int n) {
if(n = = 0 || n = = 1) return n ; // base case
return (fib(n–1) + fib(n–2)) ; // recursive case
}
E.g.2: Finding a value x in a list A [ ] using binary search
int bsearch(int l, int u, int x) { // l = 0 , u = n–1 initially
// Assume that list A[ ] is an sorted array in ascending order
int m ;
if (l > u) return –1 ; // base case
m = (l + u) / 2;
if (x = = a[ m ]) return m ; // base case
return (x > a[m] ? bsearch (m+1, u, x) : bsearch (l, m–1, x)) ; // recursive case
}
Recursive Output Problems:
Q1. Give the output when meth (3) is called?
void meth(int ctr) {
if (ctr = = 0) System.out.println(“ ”) ;
else {
System.out.println (“Hello” +ctr);
meth (– –ctr) ;
System.out.println (“ ” +ctr);
}
}
O/P:
Hello3
Hello2
Hello1
0
1
2
Q2. Give the output for num (int n) when n = 5
public void num(int n) {
if(n > 0) {
System.out.print(n+“ ”) ;
num(n – 2) ;
System.out.print(n+“ ”) ;
CITY MONTESSORI SCHOOL
SECTOR-D LDA KANPUR ROAD, LUCKNOW
PHONE: 0522 2435690 FAX: 0522 2435690
}
}
O/P:
531135
Q3. Give the output when both „m‟ and „n‟ are equal to 5.
int check(int m, int n) {
if(n = = 1)
return –m– – ;
else
return (++m + check(m, – –n)) ;
}
O/P: 21
Q4.Give the output for num1 (int n) when n = 6
public String num1(int n) {
if (n <= 0) return “ ” ;
return num1(n–1) + n + “ ” ;
}
O/P:
123456
Q5. Give the output when s = “Program”, p1 = 0, p2 = 6.
public void func(String s, int p1, int p2) {
if (p1 > p2) return ;
else {
p1++; p2– –;
func (s, p1, p2) ;
System.out.print(s.charAt(p1)) ;
System.out.print(s.charAt(p2)) ;
}
}
O/P: roggorra
Q6. What will the function check ( ) return when the value of (i) n=25 and (ii) n=10?
int check (int n) {
if (n<=1) return 1;
if (n%2 == 0) return 1 + check(n/2) ;
else return 1 + check (n/2 + 1);
}
O/P: (i) 6 (ii) 5
Q7. What will the function smile ( ) return when the value of (i) n=9465 and (ii) n=3254?
String smile (int n) {
if (n > 2) {
if (n%2 != 0)
return smile (n/10) + “A”;
CITY MONTESSORI SCHOOL
SECTOR-D LDA KANPUR ROAD, LUCKNOW
PHONE: 0522 2435690 FAX: 0522 2435690
else return smile (n/10 + 2) + “B”;
}
else return “”;
}
O/P: (i) “ABBA” (ii) “ABAB”