0% found this document useful (0 votes)
28 views59 pages

recursion in java

Recursion

Uploaded by

anshj9797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views59 pages

recursion in java

Recursion

Uploaded by

anshj9797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 59

RECURSION IN JAVA

Recursion in java is a process in


which a method calls itself
continuously.

A method in java that calls itself


is called recursive method.
SYNTAX:

 returntype methodname(){
 //code to be executed
 methodname();//calling same method
}
EXAMPLE 1: INFINITE TIMES
 public class RecursionExample1 {
 static void p(){
 System.out.println("hello");
 p();
}

 public static void main(String[] args) {
 p();
}
}
OUTPUT:

 hello
 hello
 ...
 java.lang.StackOverflowError
EXAMPLE 2: FINITE TIMES
 public class RecursionExample2 {
 static int count=0;
 static void p(){
 count++;
 if(count<=5){
 System.out.println("hello "+count);
 p();
 }
 }
 public static void main(String[] args) {
 p();
 }
 }
OUTPUT:

 hello 1
 hello 2
 hello 3
 hello 4
 hello 5
BASE CASE
 Thebase case(s), or halting case(s), are the
conditions under which a recursive function
stops recurring. The base case is the small
problem we know how to solve. Every
recursive function must have a base case in
order to stop (the base case is the point at
which the recursion halts
RECURSIVE CASE
 Recursivecase is the more general case of
the problem we are trying to solve using
recursive function
OUTPUT:

 hello 1
 hello 2
 hello 3
 hello 4
 hello 5
 Java Recursion Example 3: Factorial Number
 public class RecursionExample3 {
 static int factorial(int n){
 if (n == 1)
 return 1;
 else
 return(n * factorial(n-1));
 }

 public static void main(String[] args) {
 System.out.println("Factorial of 5 is: "+factorial(5));
 }
 }
(N * FACTORIAL(N-1));
OUTPUT:
FACTORIAL OF 5 IS: 120
 Working of above program:
 factorial(5)
 factorial(4)
 factorial(3)
 factorial(2)
 factorial(1)
 return 1
 return 2*1 = 2
 return 3*2 = 6
 return 4*6 = 24
 return 5*24 = 120
JAVA RECURSION EXAMPLE 4: FIBONACCI
SERIES
 public class RecursionExample4 {
 static int n1=0,n2=1,n3=0;
 static void printFibo(int count){
 if(count>0){
 n3 = n1 + n2;
 n1 = n2;
 n2 = n3;
 System.out.print(" "+n3);
 printFibo(count-1); } }

 public static void main(String[] args) {
 int count=15;
 System.out.print(n1+" "+n2);//printing 0 and 1
 printFibo(count-2);//n-
2 because 2 numbers are already printed } }
 Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
EUCLID'S ALGORITHM.

 The greatest common divisor (gcd) of two


positive integers is the largest integer that
divides evenly into both of them. For
example, the gcd(102, 68) = 34.
 We can efficiently compute the gcd using the
following property, which holds for positive
integers p and q:
 If p > q, the gcd of p and q is the same as the
gcd of q and p % q.
THE STATIC METHOD GCD() IN EUCLID.JAVA IS A
COMPACT RECURSIVE FUNCTION WHOSE
REDUCTION STEP IS BASED ON THIS PROPERTY.

 gcd(1440, 408)
 gcd(408, 216)
 gcd(216, 192)
 gcd(192, 24)
 gcd(24, 0)
 return 24
 return 24
 return 24
 return 24
 return 24
 public static int hcf(int n1, int n2) {
 if (n2 != 0)
 return hcf(n2, n1 % n2);
 else
 return n1; } }
TOWERS OF HANOI.

 In the towers of Hanoi problem, we have


three poles and n discs that fit onto the
poles. The discs differ in size and are initially
stacked on one of the poles, in order from
largest (disc n) at the bottom to smallest
(disc 1) at the top. The task is to move
all n discs to another pole, while obeying the
following rules:
 Move only one disc at a time.
 Never place a larger disc on a smaller one.
 Recursionprovides just the plan that we
need: First we move the top n−1 discs to an
empty pole, then we move the largest disc to
the other empty pole, then complete the job
by moving the n−1 discs onto the largest
disc. TowersOfHanoi.java is a direct
implementation of this strategy.
 The Recursion and Iteration both repeatedly
execute the set of instructions. Recursion is
when a statement in a function calls itself
repeatedly. The iteration is when a
loop repeatedly executes until the
controlling condition becomes false. The
primary difference between recursion and
iteration is that recursion is a process,
always applied to a function and iteration is
applied to the set of instructions which we
want to get repeatedly executed.
RECURSION

 Recursion uses selection structure.


 Infinite recursion occurs if the recursion step
does not reduce the problem in a manner that
converges on some condition (base case) and
Infinite recursion can crash the system.
 Recursion terminates when a base case is
recognized.
 Recursion is usually slower than iteration due to
the overhead of maintaining the stack.
 Recursion uses more memory than iteration.
 Recursion makes the code smaller.
public class RecursionExample {
public static void main(String args[]) {
RecursionExample re = new
RecursionExample();
int result = re.factorial(4);
System.out.println("Result:" + result);
}
public int factorial(int n) {
if (n==0) {
return 1;
}
else {
return n*factorial(n-1);
}
}
}
ITERATION

 Iteration uses repetition structure.


 An infinite loop occurs with iteration if the
loop condition test never becomes false and
Infinite looping uses CPU cycles repeatedly.
 An iteration terminates when the loop
condition fails.
 An iteration does not use the stack so
it's faster than recursion.
 Iteration consumes less memory.
 Iteration makes the code longer.
PROGRAMMING EXAMPLES

You might also like