C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Product of two numbers using recursion java

By : Ashlee Bennett
Date : November 21 2020, 11:01 PM
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

Date : March 29 2020, 07:55 AM
wish help you to fix your issue I thought this would be relatively easy but running it, gives wrong result. It may be the initialization of the result variable but I cant seem to be able to modify the code such as to be correct. Here is my progress so far. , better solution *here*
code :
``````public static int product(int n1, int n2)
{
if (n2 > 1)
{
n1 += product(n1,n2-1) ;
}
return n1;
}
``````

## 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

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 base-case 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, n-1);
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,n-1);
}
}
}
``````