Skip to content

Commit eb3e63a

Browse files
authored
Added recursiveFunction + PowerOf + info.md
1 parent a294a74 commit eb3e63a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

recursiveFunction/PowerOf.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Name: Mason Z.
2+
// Program: PowerOf
3+
// Date: Apr 16, 2024
4+
// Description: powerOf function + superscript function
5+
6+
import java.util.Scanner;
7+
8+
public class PowerOf {
9+
10+
11+
// run programs
12+
public static void main(String[] args) {
13+
Scanner s = new Scanner(System.in);
14+
System.out.println("what is base value?");
15+
int b = s.nextInt();
16+
System.out.println("what is the exponent? (power of)? ");
17+
int a = s.nextInt();
18+
19+
// System.out.println(b + " to the power of " + a );
20+
System.out.println(b + superscript(a) + " is " + powerOf(b, a));
21+
}
22+
23+
24+
// powerOf function
25+
// recursive function - learning example!
26+
// b = base, a = exponent
27+
private static int powerOf(int b, int a) {
28+
if (a == 0 )
29+
return 1;
30+
else
31+
return b * powerOf(b, a-1);
32+
}
33+
34+
35+
// superScript function
36+
public static String superscript(int n) {
37+
// This array stores superscript characters for digits 0-9
38+
char[] superscriptChars = {'⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'};
39+
StringBuilder sb = new StringBuilder();
40+
for (char c : String.valueOf(n).toCharArray()) {
41+
sb.append(superscriptChars[c - '0']);
42+
}
43+
return sb.toString();
44+
}
45+
46+
47+
48+
49+
50+
}

recursiveFunction/info.md.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# recursiveFunction
2+
Project written on Apr 16, 2024
3+
4+
Contains one file, PowerOf
5+
6+
Accepts two integer input in console, one as base, one as exponent
7+
Run through a powerOf function, which the recursive function that accepts these two inputs as b + a
8+
Replies with result when func end
9+
10+
Function superscript accepts an n parameter, and will return with it's superscripted form
11+
The answer replied in main will print out b and a (a in superscript form) and the answer result by calling on powerOf function.
12+
13+
14+
15+

0 commit comments

Comments
 (0)