Recursion

Recursive programming is a procedure in which a method calls itself, so that a problem is solved more and more with each method call. This continues until the problem is reduced to a very simple case.

Some tasks in computer science can be solved well by reducing a large problem step by step to smaller and smaller problems of the same kind until simple (trivial) solutions arise . From this finally the solution of the original large problem is put together.

Syntax

modifiers return_type method_name(type parameter) {
    // code to be executed
    return method_name(type parameter)
}

Example: factorial

public class FactorialRecursion {
    public static int factorial(int number) {
        if (number < 2) {
            return 1;
        } else {
            return number * factorial(number - 1);
        }
    }

    public static void main(String[] args) {
        int factorial = factorial(8);

        System.out.println("factorial of 8 is " + factorial);
    }
}
Output
factorial of 8 is 40320
Code Explanation

This is a Java program that calculates the factorial of a number using recursion.

The program defines a class FactorialRecursion that contains a method factorial that takes a single parameter, an integer number, and calculates its factorial using a recursive algorithm.

The factorial method has two branches. If number is less than 2, it returns 1, which is the base case for the recursion. If number is greater than or equal to 2, it returns number * factorial(number - 1). This line of code makes a recursive call to the factorial method, passing in number - 1 as the argument. The recursion continues until number is less than 2, at which point the base case is reached and the recursion stops.

In the main method of the FactorialRecursion class, the factorial of 8 is calculated and printed to the console.

Example: fibonacci

public class FibonacciRecursion {
    public static int fibonacciAlgorithm(int number) {
        if (number == 0 || number == 1) {
            return number;
        } else {
            return fibonacciAlgorithm(number - 1) + fibonacciAlgorithm(number - 2);
        }
    }

    public static void main(String[] args) {
        int fib = fibonacciAlgorithm(10);

        System.out.println("fibonacci number of the number 10 is " + fib);
    }
}
Output
fibonacci number of the number 10 is 55
Code Explanation

This is a Java program that calculates the nth Fibonacci number using recursion.

The program defines a class FibonacciRecursion that contains a method fibonacciAlgorithm that takes a single parameter, an integer number, and calculates the nth Fibonacci number using a recursive algorithm.

The fibonacciAlgorithm method has two branches. If number is 0 or 1, it returns number, which is the base case for the recursion. If number is greater than 1, it returns fibonacciAlgorithm(number - 1) + fibonacciAlgorithm(number - 2). This line of code makes two recursive calls to the fibonacciAlgorithm method, passing in number - 1 and number - 2 as the arguments. The recursion continues until number is 0 or 1, at which point the base case is reached and the recursion stops.

In the main method of the FibonacciRecursion class, the 10th Fibonacci number is calculated and printed to the console.