-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabRecursion1.java
More file actions
56 lines (46 loc) · 878 Bytes
/
LabRecursion1.java
File metadata and controls
56 lines (46 loc) · 878 Bytes
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
47
48
49
50
51
52
53
54
55
56
public class LabRecursion1 {
public static void main(String[] args) {
System.out.println(raiseBaseToExp(5,2));
mystery3(5);
System.out.println();
System.out.println(mystery(4));
}
public static int raiseBaseToExp(int base, int exp) {
if(exp==0)
return 1;
//5-1=4
//4-1=3
//3-1=2
//2-1=1
//1-1=0
//return 1
else
return base*raiseBaseToExp(base,exp-1);
}
public static void mystery3 (int n)
{
if (n > 0)
{
//n=5 5
//5-1=4 4
//4-1=4 3
//3-1=2 2
//2-1=1 1
//1-1=0 skips 0 goes up one
System.out.print(n + " ");
mystery3(n - 1);
} }
public static int mystery(int n) {
if (n==1)
// 4-1=3 3*27=81
// 3-1=2 3*9=27
// 2-1=1 3*3=9
//RETURN 3
return 3;
return 3 * mystery (n-1);
}
public static void mystery2 (int n) {
if (n > 0) {
mystery3(n - 1);
System.out.print(n + " "); } }
}