-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplicationRecursive.java
More file actions
46 lines (42 loc) · 1.07 KB
/
MultiplicationRecursive.java
File metadata and controls
46 lines (42 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Multiplication (Recursive)
// Send Feedback
// Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction and addition for your calculation. No other operators are allowed.
// Input format :
// Line 1 : Integer M
// Line 2 : Integer N
// Output format :
// M x N
// Constraints :
// 0 <= M <= 1000
// 0 <= N <= 1000
// Sample Input 1 :
// 3
// 5
// Sample Output 1 :
// 15
// Sample Input 2 :
// 4
// 0
// Sample Output 2 :
// 0
import java.util.Scanner;
public class MultiplicationRecursive {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number 1 : ");
int m = s.nextInt();
System.out.println("Enter number 2 : ");
int n = s.nextInt();
int ans = multiplyTwoIntegers(m, n);
System.out.println("Output : " + ans);
s.close();
}
public static int multiplyTwoIntegers(int m, int n)
{
if(n==0)
{
return 0;
}
return m + multiplyTwoIntegers(m, n-1);
}
}