Skip to content

Commit 56e8872

Browse files
add pow
1 parent a60cb58 commit 56e8872

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Maths/Pow.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package maths;
2+
3+
public class Pow {
4+
public static void main(String[] args) {
5+
assert pow(2, 0) == Math.pow(2, 0);
6+
assert pow(0, 2) == Math.pow(0, 2);
7+
assert pow(2, 10) == Math.pow(2, 10);
8+
assert pow(10, 2) == Math.pow(10, 2);
9+
}
10+
11+
/**
12+
* Returns the value of the first argument raised to the power of the
13+
* second argument
14+
*
15+
* @param a the base.
16+
* @param b the exponent.
17+
* @return the value {@code a}<sup>{@code b}</sup>.
18+
*/
19+
public static long pow(int a, int b) {
20+
long result = 1;
21+
for (int i = 1; i <= b; i++) {
22+
result *= a;
23+
}
24+
return result;
25+
}
26+
}

Maths/PowRecursion.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Maths;
2+
3+
public class PowRecursion {
4+
public static void main(String[] args) {
5+
assert pow(2, 0) == Math.pow(2, 0);
6+
assert pow(0, 2) == Math.pow(0, 2);
7+
assert pow(2, 10) == Math.pow(2, 10);
8+
assert pow(10, 2) == Math.pow(10, 2);
9+
}
10+
11+
/**
12+
* Returns the value of the first argument raised to the power of the
13+
* second argument
14+
*
15+
* @param a the base.
16+
* @param b the exponent.
17+
* @return the value {@code a}<sup>{@code b}</sup>.
18+
*/
19+
public static long pow(int a, int b) {
20+
int result = 1;
21+
for (int i = 1; i <= b; i++) {
22+
result *= a;
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)