-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursionAssignment1.java
41 lines (34 loc) · 1 KB
/
RecursionAssignment1.java
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
public class RecursionAssignment1 {
public static void main(String[] args) {
int[] arr = new int[] {
18,
21,
22,
8,
31,
1,
5
};
System.out.println(getLargestNumber(arr, arr.length - 1));
System.out.println(getGreatestCommonDivisor(24, 54));
System.out.println(reverseString("Mustangs"));
}
public static int getLargestNumber(int[] arr, int index) {
if (index == 0) {
return arr[index];
}
return Math.max(arr[index], getLargestNumber(arr, index - 1));
}
public static int getGreatestCommonDivisor(int a, int b) {
if (b == 0) {
return a;
}
return getGreatestCommonDivisor(b, a % b);
}
public static String reverseString(String str) {
if (str.isEmpty()) {
return str;
}
return reverseString(str.substring(1)) + str.charAt(0);
}
}