Java Program to Calculate the Power Using Recursion
What is Power of a Number ?
In mathematics, the power of a number refers to the number of times that number is multiplied by itself. It is represented by an exponent, which is a small number written to the right and above the base number. For example, if we have the base number 2 raised to the power of 3, it is written as 2³, which means 2 multiplied by itself 3 times:
2³ = 2 x 2 x 2 = 8
How to Calculate the Power of a Number ?
To calculate the power of a number, you simply need to multiply the base number by itself the number of times indicated by the exponent. Here’s a general formula for calculating the power of a number:
a^n = a × a × a × … (n times)
where “a” is the base number and “n” is the exponent.
For example, let’s say we want to calculate 2 raised to the power of 4. Using the formula above, we get:
2^4 = 2 × 2 × 2 × 2 = 16
Similarly, if we want to calculate 5 raised to the power of 3, we get:
5^3 = 5 × 5 × 5 = 125
So, to calculate the power of a number, all you need to do is multiply the base number by itself the number of times indicated by the exponent.
Steps to Find the Power of a Number using Recursion:
- Define a function that takes two parameters: the base number and the exponent.
- Inside the function, check if the exponent is equal to 0. If it is, return 1.
- If the exponent is not equal to 0, recursively call the function with the base number and the exponent minus 1 as arguments.
- Multiply the result of the recursive call by the base number and return the result.
Recursion Pseudo Code for the above algorithm :
public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } }
Example 1 :
public class Main { public static void main(String[] args) { int base = 2; int exponent = 4; int result = power(base, exponent); System.out.println("2 raised to the power of 4 is: " + result); } public static int power(int base, int exponent) { if(exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } }
Output :
2 raised to the power of 4 is: 16
The power method is a recursive method that takes two parameters: the base number and the exponent. If the exponent is equal to 0, the method returns 1. Otherwise, the method calculates the power of the base number raised to the exponent by recursively calling itself with the base number and the exponent minus 1 as arguments.
Example 2 :
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the base number: "); double base = scanner.nextDouble(); System.out.print("Enter the exponent: "); double exponent = scanner.nextDouble(); double result = power(base, exponent); System.out.println(base + " raised to the power of " + exponent + " is " + result); } public static double power(double base, double exponent) { if (exponent == 0) { return 1; } else if (exponent > 0) { return base * power(base, exponent - 1); } else { return 1 / power(base, -exponent); } } }
Output :
Enter the base number: 5 Enter the exponent: 2 5.0 raised to the power of 2.0 is 25.0
If the exponent is equal to 0, the method returns 1, as any number raised to the power of 0 is always equal to 1. If the exponent is greater than 0, we recursively call the power method with the base number and the exponent minus 1 as arguments, and then multiply the result of that call by the base number. We continue this process until the exponent becomes equal to 0, at which point we return the final result.
If the exponent is less than 0, we take the absolute value of the exponent, recursively call the power method with the base number and the absolute value of the exponent as arguments, and then return 1 divided by the result of that call.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment