Recursion in Java

 

Recursion is a process in which a function or a method calls itself continuously. A function or a method in java programming language which calls itself is known as recursive function or recursive method.

The code in which a recursive function is used makes the code compact but the code becomes difficult to understand. The following is the syntax of a recursive method in java programming language:

SYNTAX:

returnType nameOfMethod {

body of method;

nameOfMethod ();

}

 

Java recursion Example 1: infinite times:

Consider the following example in which the recursive function calls itself infinite number of times:

CODE:

public static ExampleRecursion {

static void x () {

system. out. println (“Hello World”);

x ();

}

public static void main (String args []) {

x ();

}

}

OUTPUT:

Hello World

Hello World

………

java. lang. StackOverflowError

In the above example the method x () is a recursive method and calls itself an infinite number of times. And the statement “Hello World” prints infinite number of times and stack over flow error is occurred.

 

Java Recursion Example 2: Finite Times:

Consider the following example in which the recursive function calls itself a finite number of times:

CODE:

public static ExampleRecursion {

static int counter = 0;

static void x () {

counter ++;

if (counter < = 4) {

system. out. println (“Hello World”);

x ();

}

}

public static void main (String args []) {

x ();

}

}

OUTPUT:

Hello World

Hello World

Hello World

Hello World

In the above example we used the “if statement” to make the recursion finite. A variable named counter is initialized at 0 and is incremented in the method then we set the condition that when the variable “counter” becomes equal to 4 then the function or method will no more call itself.

 

Java recursion Example 3: Factorial Number:

Consider the following example in which we have used function recursion to find the factorial of a number in Java programming language. By recursion of function we mean that we are calling the function in its body.

CODE:

class ExampleFactorial {

static int fact (int n) {

if (n == 0) {

return 1;

}

else {

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

}

public static void main (string [] args) {

int j, factorial = 1;

int x = 4;

factorial = fact (x);

system. out. println (“factorial of entered number is:” + factorial);

}

}

OUTPUT:

Factorial of entered number is 24

In the above example, we defined a function to calculate the factorial of the number and the function calls itself and hence we did not need to use any looping construct in our program. As the factorial of the number ‘0’ is 1, therefore, we set a condition that when the number is 0 then compiler should return 1 and in the else condition we returned the calculated factorial of the number.

The statement (n * fact (n-1)) calculates the factorial, in which function calls itself by passing the argument (n-1) and multiplies it with the number ‘n’. In the main function three variables are declared. The variable ‘x’ is the number whose value is to be calculated and is passed to the function. The variable ‘factorial’ is used to store the result. Then we used the print statement to print the factorial that was stored in the variable ‘factorial’.

 

Java recursion Example 4: Fibonacci series:

Consider the following example in which we have used recursion to implement the Fibonacci series:

CODE:

class ExampleFibonacci {

static int a = 0, b = 1, c = 0;

static void Fibonacci (int count) {

if (count > 0) {

c = a + b;

a = b;

b = c;

system. out. print (“ ” +c);

Fibonacci (count-1);

}

}

public static void main (string [] args) {

int count = 10;

system. out. print (a+ “ ” +b);

Fibonacci (count-2);

}

}

OUTPUT:

0 1 1 2 3 5 8 13 21 34

A recursive function is that which is called in its body. In the above example we used the recursive function for the implementation of Fibonacci series.