java
•Write a method product(int x, int y) which returns the product of multiplying x * y but doing it recursively. Recall that multiplication can be implemented as repeated addition. For example, 4 * 7 is the same as adding 7, 4 times: 7 + 7 + 7 + 7. •Write a main method that asks for two numbers, and returns their product by calling the product method.
Compile and test your code in NetBeans and then on Hackerrank
//MultiplyRec.java import java.util.Scanner; public class MultiplyRec { public static int multiply(int x, int y){ if(x == 0){ return 0; } else{ return y + multiply(x-1,y); } } public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter number: "); int n = scan.nextInt(); System.out.print("Enter number: "); int m = scan.nextInt(); System.out.println("RESULT: "+multiply(n,m)); } }
Get Answers For Free
Most questions answered within 1 hours.