hope this fix your issue You are correct in your explanation of how the method works, but remember, since this is recursive, it ends up returning the total of all returns. Let me explain: The first recursion returns 3 + product(3, 1). However, your program does not know what product(3, 1) is yet, so it must continue before returning. The second recursion (product(3, 1)) returns 3 + product(3, 0). Again, your program must continue the recursion to find product(3, 0). The third and final recursion returns 0, as you said, but the FINAL answer goes back to the first recursion's answer, 3 + product(3, 1). We know that product(3, 1) = 3 + product(3, 0), and product(3, 0) = 0. So product(3, 2) = 3 + 3 + 0, which is 6. It appears in your answer that you were expecting only to receive the final recursion for an answer, but because of your return statement, it is a combination of the three. code :
Share :

Getting product and quotient of 2 numbers using recursion without using the * and / operators
By : KingPenguin
Date : March 29 2020, 07:55 AM
like below fixes the issue I am trying to learn programming and I am on the phase of learning recursion. Before this, I have successfully solve the problem but using loops. Right now, since recursion is quite interesting to me, I was wondering if I could convert the loop into a recursive method. I have done my attempts but I've been getting a sort of infinite computation whatsoever. , Try this: code :
public static int recursiveProduct(int a, int b) {
if (b == 0) {
return 0;
} else {
return a + recursiveProduct(a,b);
}
}

Recursion related: Product of two numbers
By : Hesham Fouad
Date : March 29 2020, 07:55 AM

Sum of numbers using recursion java
By : Mark Tate
Date : March 29 2020, 07:55 AM
hope this fix your issue The main problem is that you are always recursing when sum != n. When the sum gets bigger than n, you never stop, hence the StackOverflowError This means we need to add a check and terminate when the sum gets bigger: code :
public static void test_2(String path, int sum, int n) {
if (sum == n) {
System.out.println(path);
} else if (sum < n) { // < only recurse if the sum is less than the target
test_2(path+"1 ", sum + 1, n);
test_2(path+"2 ", sum + 2, n);
test_2(path+"3 ", sum + 3, n);
test_2(path+"4 ", sum + 4, n);
}
}
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1
4

Product of two arrays using recursion Java
By : jaladi thirumala nag
Date : March 29 2020, 07:55 AM
Hope that helps The auxiliary method is a good suggestion, but with a different method signature than that you listed in your code. Reason being is that you need to keep track of the index less than n, then your basecase for the recursion is when that index is greater than n. code :
import java.util.Arrays;
public class Product {
// auxiliary method
private static void _arrayProd(int[] v1, int[] v2, int[] result, int n, int i) {
if(i < n){
result[i] = v1[i] * v2[i];
_arrayProd(v1, v2, result, n, i+1);
}
}
public static int[] arrayProd(int[] v1, int[] v2, int n) {
int[] result = null;
if(n >= 0){
result = new int[n];
_arrayProd(v1, v2, result, n, 0);
}
return result;
}
public static void main(String [] args) {
int[] v1 = {1, 2, 3, 4, 5};
int[] v2 = {2, 4, 6, 8};
int n = 3;
int[] v3 = arrayProd(v1, v2, n);
System.out.println(Arrays.toString(v3));
}
}

understanding recursion for dot product in java
By : Samcro
Date : March 29 2020, 07:55 AM
Any of those help Ok so hoping you have tried it before this is one possible way to code it. code :
public class ArrayExample {
public static void main (String[] args) {
int[] a = {2,2,2,2};
int[] b = {2,2,2,2};
int n = a.length;
int result = recurseSum(a, b, n1);
System.out.println(result);
}
public static int recurseSum(int[] a, int[] b, int n){
if(n == 0)
return a[0]*b[0];
else{
return (a[n] * b[n]) + recurseSum(a,b,n1);
}
}
}

