-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion.java
executable file
·101 lines (67 loc) · 1.24 KB
/
Recursion.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class Recursion
{
public Recursion() {}
public int sumOfNumbersTo(int n)
{
if(n==0)
return 0;
else
return n + sumOfNumbersTo(n-1);
}
public int sixTimes(int n)
{
if(n==1)
return 1;
else
return 6 * sixTimes(n-1);
}
public int mutiply(int x, int y)
{
if(y==0)
return 0;
else
return x + mutiply(x, y -1);
}
public int div4(int p)
{
if(p<4)
return 0;
else if(p % 4 == 0)
return p;
else
return div4(p -1);
}
public int HighestXDivByY(int x, int y)
{
if(y>x)
return 0;
else if(x % y == 0)
return x;
else
return HighestXDivByY(x-1, y);
}
public static void main(String args[])
{
Recursion r = new Recursion();
// sum 1 to 6
int x = r.sumOfNumbersTo(6);
System.out.println(x);
// 6 * n
int p = r.sixTimes(3);
System.out.println(p);
// multiply x by y
int g = r.mutiply(3,6);
System.out.println(g);
// highest number divisible by 4 in p
int h = r.div4(47);
System.out.println(h);
// highest number in X divisible to Y
int q = r.HighestXDivByY(4,2);
System.out.println(q);
String name = "john";
if(name == "john")
{
System.out.println("yes");
}
}
}