|
@@ -3,21 +3,47 @@ package io.zipcoder.microlabs.mastering_loops;
|
3
|
3
|
|
4
|
4
|
public class NumberUtilities {
|
5
|
5
|
public static String getEvenNumbers(int start, int stop) {
|
6
|
|
- return null;
|
|
6
|
+ StringBuilder even = new StringBuilder();
|
|
7
|
+ for (int i = start; i < stop; i++) {
|
|
8
|
+ if (i % 2 == 0) {
|
|
9
|
+ even.append(i);
|
|
10
|
+ }
|
|
11
|
+
|
|
12
|
+ }
|
|
13
|
+ return even.toString();
|
7
|
14
|
}
|
8
|
15
|
|
9
|
16
|
|
10
|
17
|
public static String getOddNumbers(int start, int stop) {
|
11
|
|
- return null;
|
12
|
|
- }
|
|
18
|
+ StringBuilder odd = new StringBuilder();
|
|
19
|
+ for (int i = start; i < stop; i++) {
|
|
20
|
+ if (i % 2 == 0) {
|
|
21
|
+ odd.append(i);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ }
|
|
25
|
+ return odd.toString();
|
13
|
26
|
|
14
|
27
|
|
|
28
|
+ }
|
|
29
|
+
|
15
|
30
|
public static String getSquareNumbers(int start, int stop, int step) {
|
16
|
|
- return null;
|
|
31
|
+ StringBuilder squared = new StringBuilder();
|
|
32
|
+ for (int i = start; i < stop; i += step) {
|
|
33
|
+
|
|
34
|
+ squared.append(i * i);
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ return squared.toString();
|
|
40
|
+
|
17
|
41
|
}
|
18
|
42
|
|
19
|
43
|
public static String getRange(int start) {
|
|
44
|
+
|
20
|
45
|
return null;
|
|
46
|
+
|
21
|
47
|
}
|
22
|
48
|
|
23
|
49
|
public static String getRange(int start, int stop) {
|
|
@@ -26,7 +52,15 @@ public class NumberUtilities {
|
26
|
52
|
|
27
|
53
|
|
28
|
54
|
public static String getRange(int start, int stop, int step) {
|
29
|
|
- return null;
|
|
55
|
+ StringBuilder rangeNum = new StringBuilder();
|
|
56
|
+ for (int i = start; i < stop; i += step) {
|
|
57
|
+
|
|
58
|
+ rangeNum.append(i);
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+ }
|
|
62
|
+ return rangeNum.toString();
|
|
63
|
+
|
30
|
64
|
}
|
31
|
65
|
|
32
|
66
|
|