Q:

how to change code this into recursion Java?

public long weightedSum(int[] a, int[] b, int n) {
    long value = 0;
    long sum = 0;

    for (int i = 0; i < a.length; i++) {
        value = a[i] * b[i];
        sum = sum + value;

    }

    return sum;
}

takes two 1D integer arrays and an integer n as parameters and returns the sum of products of the first n elements of the two arrays. For example, given the following two 1D arrays:
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {6, 7, 8, 9, 10};

the first 4 elements of arr1 and arr2, that is,1 * 6 + 2 * 7 + 3 * 8 + 4 * 9 = 80 as the result.

A:

public static long weightedSum(int[] a, int[] b, int n) {
    if (n == 0)
        return 0;
    else
        return a[n - 1] * b[n - 1] + weightedSum(a, b, n - 1);
}

Output :
int[] arr1 = { 1, 2, 3, 4, 5 };
int[] arr2 = { 6, 7, 8, 9, 10 };

System.out.println(weightedSum(arr1, arr2, 1));  // output : 6
System.out.println(weightedSum(arr1, arr2, 2));  // output : 20
System.out.println(weightedSum(arr1, arr2, 3));  // output : 44
System.out.println(weightedSum(arr1, arr2, 4));  // output : 80
System.out.println(weightedSum(arr1, arr2, 5));  // output : 130

