Assignment: Create a methodcalled, rangeMult, that uses recursion to multiply a
ID: 3619081 • Letter: A
Question
Assignment: Create a methodcalled, rangeMult, that uses recursion to multiply a range of arrayelements. The method takes the following arguments: an int arraythat contains the range of elements to be multiplied, an intspecifying the starting element of the range, and an int specifyingthe ending element of the range. Here is an example of how themethod could be used:
int[] numbers = { 1, 2, 3, 4, 5 };
int sum;
sum = rangeMult(numbers, 1, 3);
public class Recursion{
public static void main(String[] args)
{
int[] numbers ={1,2,3,4,5};
System.out.println("The sum of multiplication is " +rangeMulti(numbers,1,3));
}
publicstatic int rangeMulti(int[] numbers, int start, int end)
{
if(start > end)
return 1;
else
return numbers[start] * rangeMulti(numbers,start+1,end);
}
}
Explanation / Answer
please rate - thanks remember you are sending the element number, not the index number,I've highlighted the ranges public class Recursion { public static void main(String[] args) { int[] numbers ={1,2,3,4,5}; System.out.println("Thesum of multiplication is " + rangeMulti(numbers,1,3)); } public static int rangeMulti(int[] numbers, intstart, int end) { if(start == end) return start; else return numbers[start-1] *rangeMulti(numbers,start+1,end); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.