|
@@ -3,17 +3,43 @@ 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
|
+
|
|
8
|
+ if (start % 2 != 0) {
|
|
9
|
+ start += 1;
|
|
10
|
+ }
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+ for (int i = start; i < stop; i += 2) {
|
|
14
|
+ even.append(i);
|
|
15
|
+ }
|
|
16
|
+ return even.toString();
|
7
|
17
|
}
|
8
|
18
|
|
9
|
19
|
|
10
|
20
|
public static String getOddNumbers(int start, int stop) {
|
11
|
|
- return null;
|
|
21
|
+ StringBuilder odd = new StringBuilder();
|
|
22
|
+
|
|
23
|
+ if (start % 2 == 0) {
|
|
24
|
+ start -= 1;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+ for (int i = start; i < stop; i += 2) {
|
|
29
|
+ odd.append(i);
|
|
30
|
+ }
|
|
31
|
+ return odd.toString();
|
12
|
32
|
}
|
13
|
33
|
|
14
|
34
|
|
15
|
35
|
public static String getSquareNumbers(int start, int stop, int step) {
|
16
|
|
- return null;
|
|
36
|
+ StringBuilder square = new StringBuilder();
|
|
37
|
+ for (int i = start; i < stop; i += step) {
|
|
38
|
+ int squarednum = (int) Math.pow(i, 2);
|
|
39
|
+ square.append(squarednum);
|
|
40
|
+
|
|
41
|
+ }
|
|
42
|
+ return square.toString();
|
17
|
43
|
}
|
18
|
44
|
|
19
|
45
|
public static String getRange(int start) {
|
|
@@ -26,11 +52,21 @@ public class NumberUtilities {
|
26
|
52
|
|
27
|
53
|
|
28
|
54
|
public static String getRange(int start, int stop, int step) {
|
29
|
|
- return null;
|
|
55
|
+ StringBuilder range = new StringBuilder();
|
|
56
|
+ for (int i = start; i < stop; i += step) {
|
|
57
|
+ range.append(i);
|
|
58
|
+ }
|
|
59
|
+ return range.toString();
|
30
|
60
|
}
|
31
|
61
|
|
32
|
62
|
|
33
|
63
|
public static String getExponentiations(int start, int stop, int step, int exponent) {
|
34
|
|
- return null;
|
|
64
|
+ StringBuilder expo = new StringBuilder();
|
|
65
|
+ for (int i = start; i < stop; i += step) {
|
|
66
|
+ int exponentVariable = exponent;
|
|
67
|
+ int squarednum = (int) Math.pow(i, exponentVariable);
|
|
68
|
+ expo.append(squarednum);
|
|
69
|
+ }
|
|
70
|
+ return expo.toString();
|
35
|
71
|
}
|
36
|
|
-}
|
|
72
|
+}
|