In this Java program, we will learn how to calculate the power using recursion in your Java program.
How to calculate the power using recursion in JAVA?
Example 1: using recursion
RUN CODE SNIPPETclass Main
{
public static void main(String[] args)
{
int base = 3, powerRaised = 5;
int result = power(base, powerRaised);
System.out.println(base + "^" + powerRaised + "=" + result);
}
public static int power(int base, int powerRaised)
{
if (powerRaised != 0)
{
return (base * power(base, powerRaised - 1));
}
else {
return 1;
}
}
}OUTPUT
3^5=243