Skip to content

Commit 80a4435

Browse files
Add tests for power using recursion algorithm (TheAlgorithms#4335)
1 parent ebd356e commit 80a4435

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/main/java/com/thealgorithms/maths/PowRecursion.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* calculate Power using Recursion
5+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
public class PowerUsingRecursion {
9+
10+
public static double power(double base, int exponent) {
11+
// Base case: anything raised to the power of 0 is 1
12+
if (exponent == 0) {
13+
return 1;
14+
}
15+
16+
// Recursive case: base ^ exponent = base * base ^ (exponent - 1)
17+
// Recurse with a smaller exponent and multiply with base
18+
return base * power(base, exponent - 1);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Test case for Power using Recursion
9+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
10+
*/
11+
12+
class PowerUsingRecursionTest {
13+
14+
@Test
15+
void testPowerUsingRecursion() {
16+
assertEquals(32.0, PowerUsingRecursion.power(2.0, 5));
17+
assertEquals(97.65625, PowerUsingRecursion.power(2.5, 5));
18+
assertEquals(81, PowerUsingRecursion.power(3, 4));
19+
}
20+
}

0 commit comments

Comments
 (0)