|
@@ -3,17 +3,30 @@ 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
|
+git
|
|
7
|
+ StringBuilder evensOnly = new StringBuilder();
|
|
8
|
+ for(int i = start; i <= stop; i++) {
|
|
9
|
+ if(i % 2 == 0) {
|
|
10
|
+ evensOnly.append(i);
|
|
11
|
+ }
|
|
12
|
+ }
|
|
13
|
+ return evensOnly.toString();
|
7
|
14
|
}
|
8
|
15
|
|
9
|
16
|
|
10
|
17
|
public static String getOddNumbers(int start, int stop) {
|
11
|
|
- return null;
|
|
18
|
+ StringBuilder oddsOnly = new StringBuilder();
|
|
19
|
+ for(int i = start; i <= stop; i++) {
|
|
20
|
+ if(i % 2 != 0) {
|
|
21
|
+ oddsOnly.append(i);
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return oddsOnly.toString();
|
12
|
25
|
}
|
13
|
26
|
|
14
|
27
|
|
15
|
28
|
public static String getSquareNumbers(int start, int stop, int step) {
|
16
|
|
- return null;
|
|
29
|
+ return getExponentiations(start, stop, step, 2);
|
17
|
30
|
}
|
18
|
31
|
|
19
|
32
|
public static String getRange(int start) {
|
|
@@ -26,11 +39,19 @@ public class NumberUtilities {
|
26
|
39
|
|
27
|
40
|
|
28
|
41
|
public static String getRange(int start, int stop, int step) {
|
29
|
|
- return null;
|
|
42
|
+ StringBuilder range = new StringBuilder();
|
|
43
|
+ for(int i = start; i < stop; i += step) {
|
|
44
|
+ range.append(i);
|
|
45
|
+ }
|
|
46
|
+ return range.toString();
|
30
|
47
|
}
|
31
|
48
|
|
32
|
49
|
|
33
|
50
|
public static String getExponentiations(int start, int stop, int step, int exponent) {
|
34
|
|
- return null;
|
|
51
|
+ StringBuilder exponentials = new StringBuilder();
|
|
52
|
+ for(int i = start; i < stop; i += step) {
|
|
53
|
+ exponentials.append((int)Math.pow(i, exponent));
|
|
54
|
+ }
|
|
55
|
+ return exponentials.toString();
|
35
|
56
|
}
|
36
|
57
|
}
|