Recursion

Recursion

Create a method called, rangeMult, that uses recursion to multiply a range of array elements. The method takes the following arguments: an int array that contains the range of elements to be multiplied, an int specifying the starting element of the range, and an int specifying the ending element of the range. Here is an example of how the method could be used:

int[] numbers = { 1, 2, 3, 4, 5 };
int sum;
sum = rangeMult(numbers, 0, 2);

The program would return 6 (i.e. 1*2*3).

Thank You
View Answers

June 11, 2010 at 4:21 PM

Hi Friend,

Try the following code:

class RecursionMethod{
public static int rangeMult(int num[], int start, int end){
if(start > end){
return 1;
}
else{
return num[start] * rangeMult(num,start+1,end);
}
}

public static void main(String[] args) {
int[] num = { 1, 2, 3, 4, 5 };
int result=RecursionMethod.rangeMult(num,0,2);
System.out.println(result);
}
}

Thanks









Related Tutorials/Questions & Answers:
recursion
recursion program
recursion program  Hi this is my first java class, and i have been trying for hours to do this program. it is a recursion problem where the user will enter a continuous monthly investment, at a rate of 1% the program should say
Advertisements
ModuleNotFoundError: No module named 'recursion'
ModuleNotFoundError: No module named 'recursion'  Hi, My Python... 'recursion' How to remove the ModuleNotFoundError: No module named 'recursion' error? Thanks   Hi, In your python environment you
recursion numbers - Java Beginners
recursion numbers  I need to use recursion to test all values from 0 to 20 and see if they are contain in a 1-D array with values: 2,4,6,8,10,12,14,16,18,20. The results of all numbers from 0-20 will be printed
javascript recursion example
javascript recursion example   javascript recursion example   <html> <script> function factorial (n) { if(n==0) return(1); return (n * factorial (n-1) ); } document.write(factorial(5)); <
recursion method - Ajax
recursion method  Can i ask for a program that input a lines in the diamond shape recursion method   Hi friend, Code for solving the problem : public class RecursiveDiamond { public RecursiveDiamond
ModuleNotFoundError: No module named 'dongjun_recursion'
ModuleNotFoundError: No module named 'dongjun_recursion'  Hi, My... named 'dongjun_recursion' How to remove the ModuleNotFoundError: No module named 'dongjun_recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'hjc_recursion'
ModuleNotFoundError: No module named 'hjc_recursion'  Hi, My... 'hjc_recursion' How to remove the ModuleNotFoundError: No module named 'hjc_recursion' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'python-recursion'
ModuleNotFoundError: No module named 'python-recursion'  Hi, My... named 'python-recursion' How to remove the ModuleNotFoundError: No module named 'python-recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-recursion'
ModuleNotFoundError: No module named 'python-recursion'  Hi, My... named 'python-recursion' How to remove the ModuleNotFoundError: No module named 'python-recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-recursion'
ModuleNotFoundError: No module named 'python-recursion'  Hi, My... named 'python-recursion' How to remove the ModuleNotFoundError: No module named 'python-recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-recursion'
ModuleNotFoundError: No module named 'python-recursion'  Hi, My... named 'python-recursion' How to remove the ModuleNotFoundError: No module named 'python-recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'Recursion-abhi'
ModuleNotFoundError: No module named 'Recursion-abhi'  Hi, My... named 'Recursion-abhi' How to remove the ModuleNotFoundError: No module named 'Recursion-abhi' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'recursion-chintan'
ModuleNotFoundError: No module named 'recursion-chintan'  Hi, My... named 'recursion-chintan' How to remove the ModuleNotFoundError: No module named 'recursion-chintan' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'recursion-detect'
ModuleNotFoundError: No module named 'recursion-detect'  Hi, My... named 'recursion-detect' How to remove the ModuleNotFoundError: No module named 'recursion-detect' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'recursion-visualiser'
ModuleNotFoundError: No module named 'recursion-visualiser'  Hi...: No module named 'recursion-visualiser' How to remove the ModuleNotFoundError: No module named 'recursion-visualiser' error? Thanks   Hi
ModuleNotFoundError: No module named 'recursion_xxx'
ModuleNotFoundError: No module named 'recursion_xxx'  Hi, My... 'recursion_xxx' How to remove the ModuleNotFoundError: No module named 'recursion_xxx' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dongjun_recursion'
ModuleNotFoundError: No module named 'dongjun_recursion'  Hi, My... named 'dongjun_recursion' How to remove the ModuleNotFoundError: No module named 'dongjun_recursion' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'hjc_recursion'
ModuleNotFoundError: No module named 'hjc_recursion'  Hi, My... 'hjc_recursion' How to remove the ModuleNotFoundError: No module named 'hjc_recursion' error? Thanks   Hi, In your python
Recursion - Java Beginners
Recursion  HI!Can i ask for another recursion diamond,using recursive method and asking for how many lines should be inputed to create the diamond..., This is simple code of recursion code. public class RecursiveDemo { public static
Recursion - Java Beginners
Recursion  Create a method called, rangeMult, that uses recursion to multiply a range of array elements. The method takes the following arguments: an int array that contains the range of elements to be multiplied, an int
ModuleNotFoundError: No module named 'HS-recursion-List'
ModuleNotFoundError: No module named 'HS-recursion-List'  Hi, My... named 'HS-recursion-List' How to remove the ModuleNotFoundError: No module named 'HS-recursion-List' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'HS-recursion-List'
ModuleNotFoundError: No module named 'HS-recursion-List'  Hi, My... named 'HS-recursion-List' How to remove the ModuleNotFoundError: No module named 'HS-recursion-List' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'HS-recursion-List'
ModuleNotFoundError: No module named 'HS-recursion-List'  Hi, My... named 'HS-recursion-List' How to remove the ModuleNotFoundError: No module named 'HS-recursion-List' error? Thanks   Hi, In your
XII STD RECURSION WITHOUT SCANNER
XII STD RECURSION WITHOUT SCANNER  the recursive function gives a stack overflow error. I want to calculate the GDC ie the greatest Integer function for two input numbers. the code of my program is as follows and the error comes
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
Calculate factorial Using Recursion
Calculate factorial Using Recursion  ... by using recursion in jsp. To make a program on factorial, firstly it must be clear what is recursion. In a simple language we can define recursion
java recursion
Multiply a range of array elements using Recursion
Multiply a range of array elements using Recursion In this section you will learn how to use recursion to multiply a range of array elements. For this we have created a method rangeMult() that takes three arguments: an int array
Recursion - Java Beginners
find factorial of any number using recursion
Find sum of series
Find sum of series This section illustrates you how to find the sum of n terms of the series using recursion. For this purpose, we have created a recursive function that takes the parameter 'n' up to which the sum of the series
PHP Loop Function
Example 2 (Recursion): <?phpADS_TO_REPLACE_8 function factorial($f
To check a palindrome number?
To check a palindrome number?  program to check a number is palindrome or not using recursion?(wihout converting to string
factorial using recursive
to do a factorial function using recursion definition and then . my professor told... an MAIN MENU then above that RECURSION below that again the EXIT BUTTON.. then second screen SHOULD BE INPUT an meaning of recursion and about information
On string - Java Beginners
/StringReverseUsingStringUtils.shtml  public class recursion{ public String reverse(String str...(String args[]) { recursion r = new recursion(); System.out.println(r.reverse
about regular expression - Java Beginners
about regular expression  Consider the grammar G: S -> ( L ) | a L -> L, S | S a) Eliminate the left-recursion from grammar G. b) Construct a recursive descent parser for G
Counter program for java
Counter program for java  The problem is to count from 000 to 444 with recursion. The maximum digit is 4 so that the program will count from 000,001,002,003,004,010, 011 and so on. How can I solve
recursionarray - Java Beginners
recursionarray  I need to initialize a 1-D array with the values: 2,4,6,8,10,12,18,20 using recursion to test all values from 0 to 20 and see if they are contained in the array. I will have to print the results out for all
c programing
c programing  Write 2 implementations of C's string copy routine, one with a loop and the other using recursion. What is the security risk with this routine? The function should have the signature: char* strcpy(char* destination
c programing
c programing  Write 2 implementations of C's string copy routine, one with a loop and the other using recursion. What is the security risk with this routine? The function should have the signature: char* strcpy(char* destination
Stack Overflow - Java Tutorials
for stack overflow are given below : infinite recursion allocating size greater than stack's limit 1. Infinite Recursion The most common reason of stack overflow is Infinite Recursion. The infinite loop of recursion going
recursions in gui
simple drawing primitives and recursion to help divide the space into smaller... and the region is taller than half the initial height: Use recursion to split... the initial size: Use recursion to split the region into 2 smaller regions using
Java Program - Java Beginners
Java Program  Write a program to find the Factorial of a number using Recursion. Factorial can be defined as Factorial(n) = 1 * 2 * 3 â?¦.* (n-1) * n.  Hi Friend, Try the following code: class

Ads