Question
I am trying to learn programming (taking a class) and do everything on my own. We have a weekly quiz where we need to solve
I am trying to learn programming (taking a class) and do everything on my own. We have a weekly quiz where we need to solve a problem by writing a short program that we do not know in advance but will be similar to problems done for homework. I cannot solve the problem below and need a hint. I do not want the entire program as then I won't be able to do it in class but I am stuck.
I need to multiply numbers recursively using only addition no multiplication, division (not sure about this but assuming for now), modulus etc. I am trying to create program that will take two numbers a and b and add b a times. I have failed miserably. I need some hints to get going including wondering of this approach would work. The exact problem and my code are below:
Follow the input/output and naming specications precisely. In particular, for your nal submission your program should not print prompts for input or add text such as \The answer is:". To receive credit for a problem that asks for a recursive solution, you must use recursion. Write a recursive method that multiplies two positive integers without using loops or the Java multiplication operator. You may assume that the rst argument to the method is no greater than the second; in the call mult(a; b), assume that 1 <= a <= b < 109.
Create a Java program called RecursiveMultiply.java that uses your method to compute the product of two input numbers. The input is two positive integers read from the standard input, the rst less than or equal to the second, and the output is the product. The output should be a number appearing on a line by itself. Your method should take int arguments, and return a long value. For example, if the input is
15 20
then the output should be
300
Your program should be effcient. Time your program on the input
20000000 20000000
For full credit, your program should compute this product in under 10 milliseconds on the OSL machines. Your program may not use loops or the Java multiplication operator; make sure that your le does not contain the character or the words \for" or \while".
Upload your solution to Vocareum.
For large numbers, the recursion may become too deep for the default Java
stack size. You can increase this, for example to 1 gigabyte, by using the compiler
argument -Xss1g. For example,
java -Xss1g RecursiveMultiply
With Eclipse, you would click on Run, then Run Congurations, and enter -Xss1g
under \VM arguments".
My code for Addtion it actually runs and gives me 174.
public class AddNumbers {
public static void main(String[] args) {
int number1 = 10;
int number2 = 20;
int sum = addNumbers(number1, number2);
System.out.println("Sum = " + sum);
}
public static int addNumbers(int num1, int num2){
if (num2 >= 0 && num1<=num2){
return num2 + addNumbers(num1, num2 - 1);
}
else
return num2;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started