mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
23 lines
616 B
Java
23 lines
616 B
Java
package com.thealgorithms.maths;
|
|
|
|
/**
|
|
* calculate Power using Recursion
|
|
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
|
*/
|
|
|
|
public final class PowerUsingRecursion {
|
|
private 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);
|
|
}
|
|
}
|