Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathProduct.java
More file actions
Latest commit
34 lines (28 loc) · 1.08 KB
/
Copy pathProduct.java
File metadata and controls
34 lines (28 loc) · 1.08 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
importjava.util.Scanner;
publicclassProduct {
publicstaticintrecursiveProd (intnum1, intnum2) {
if (num2 == 1)
returnnum1;
// If any of the numbers is zero, return 0
if (num1 == 0 || num2 == 0)
return0;
// If both numbers are less than zero negative signs can be removed
if (num1 < 0 && num2 < 0)
returnrecursiveProd (-1*num1, -1*num2);
elseif (num2 < 0)
returnrecursiveProd (num2, num1);
else
returnnum1 + recursiveProd(num1, num2-1);
}
publicstaticvoidmain(String[] args) {
Scannerinput = newScanner (System.in);
System.out.println("/* ===== Product of numbers using recursion ===== */");
// Take input
System.out.print("\nEnter first number: ");
intnum1 = input.nextInt();
System.out.print("Enter second number: ");
intnum2 = input.nextInt();
// Print the result
System.out.println("Product of numbers " + num1 + " and " + num2 + " is: " + recursiveProd(num1, num2));
}
}