File tree Expand file tree Collapse file tree 1 file changed +22
-12
lines changed Expand file tree Collapse file tree 1 file changed +22
-12
lines changed Original file line number Diff line number Diff line change 1
1
// Name: Mason Z.
2
2
// Program: PowerOf
3
- // Date: Apr 16 , 2024
3
+ // Date: Apr 24 , 2024
4
4
// Description: powerOf function + superscript function
5
5
6
6
import java .util .Scanner ;
@@ -14,23 +14,38 @@ public static void main(String[] args) {
14
14
System .out .println ("what is base value?" );
15
15
int b = s .nextInt ();
16
16
System .out .println ("what is the exponent? (power of)? " );
17
- int a = s .nextInt ();
17
+ s .nextLine ();
18
+ String c = s .nextLine ();
19
+
20
+ int a ;
21
+
22
+ // if negative exponent
23
+ if (c .contains ("-" )) {
24
+ c = c .substring (1 );
25
+ a = Integer .valueOf (c );
26
+ System .out .println (b + " to the power of " + "-" + a + " is " + "1/" + powerOf (b , a ));
27
+ }
18
28
19
- // System.out.println(b + " to the power of " + a );
20
- System .out .println (b + superscript (a ) + " is " + powerOf (b , a ));
21
- }
29
+ // if positive exponent or zero
30
+ else {
31
+ a = Integer .valueOf (c );
32
+ System .out .println (b + " to the power of " + a + " is " + powerOf (b , a ));
33
+ }
22
34
35
+
36
+ }
37
+
23
38
24
39
// powerOf function
25
40
// recursive function - learning example!
26
41
// b = base, a = exponent
27
42
private static int powerOf (int b , int a ) {
28
- if (a == 0 )
43
+ if (a == 0 )
29
44
return 1 ;
30
45
else
31
46
return b * powerOf (b , a -1 );
32
47
}
33
-
48
+
34
49
35
50
// superScript function
36
51
public static String superscript (int n ) {
@@ -42,9 +57,4 @@ public static String superscript(int n) {
42
57
}
43
58
return sb .toString ();
44
59
}
45
-
46
-
47
-
48
-
49
-
50
60
}
You can’t perform that action at this time.
0 commit comments