mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add tests for power using recursion algorithm (#4335)
This commit is contained in:
committed by
GitHub
parent
ebd356e182
commit
80a4435038
@@ -0,0 +1,20 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/**
|
||||
* calculate Power using Recursion
|
||||
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
||||
*/
|
||||
|
||||
public class PowerUsingRecursion {
|
||||
|
||||
public static double power(double base, int exponent) {
|
||||
// Base case: anything raised to the power of 0 is 1
|
||||
if (exponent == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Recursive case: base ^ exponent = base * base ^ (exponent - 1)
|
||||
// Recurse with a smaller exponent and multiply with base
|
||||
return base * power(base, exponent - 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user