Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
873 B
2
Indexable
Never
public class RecursionExamples {
    public static int findMax(int[] arr, int n) {
        if (n == 1) {
            return arr[0];
        } else {
            int max = findMax(arr, n - 1);
            if (max > arr[n - 1]) {
                return max;
            } else {
                return arr[n - 1];
            }
        }
    }
     public static int fibonacci(int n) {
        if (n == 0 || n == 1) {
            return 1;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }
     public static void main(String[] args) {
        int[] arr = {10, 5, 20, 15, 25};
        int n = arr.length;
        int max = findMax(arr, n);
        System.out.println("Maximum element in the array: " + max);
         int fib = fibonacci(6);
        System.out.println("6th Fibonacci number: " + fib);
    }
}