In: Computer Science
Guided Assignment Problem 1: Multiplication
Review the Three-Question Approach used to verify the Factorial recursive algorithm in Ch. 3.2.
Your Tasks:
Recursive method header given as: int multiplication (int a, int b)
Hints
Code:
Code:
import java.util.Scanner;
public class Main {
// this method takes two arguments
// calculates multiplication recursive approach
// return the final result
static int multiplication (int a, int b) {
// check if the value of a is
>0
// if it is greater then again call the multiplication
function
if(a>0) {
return b +
multiplication(a-1, b);
}
// else return 0
else {
return 0;
}
}
public static void main(String[] args) {
// declate two variables a and b
int a,b;
// create an object for scanner class
Scanner in = new Scanner(System.in);
// read the input from the user
System.out.print("Enter a value: ");
a = in.nextInt();
System.out.print("Enter b value: ");
b = in.nextInt();
// output the result by calling the
multiplication(a,b)
System.out.println(multiplication(a, b));
}
}