Java Program to Calculate the Power of a Number using Recursion - The Coding Shala

Home >> Java Programs >> Power of a Number using Recursion

 In this post, we will learn how to find the Power of a Number using Recursion in Java.

Java Program to Calculate the Power of a Number using Recursion

You have given a number N ( base) and power p (exponent). Write a Java Program to Calculate the power of the number N by using recursion.

Example 1:
Input: N = 3
          P = 4
Output: 81

Java Program: 

// Java program to calculate power of a number using Recursion

public class Main {
	
	public static int findPower(int base, int exponent) {
		if(exponent == 0) {
			return 1;
		}
		return base * findPower(base, exponent - 1);
	}
	
	public static void main(String[] args) { 
		// inputs can be taken using scanner
		int base = 3;
		int exponent = 4;
		
		// using recursion
		int power = findPower(base, exponent);
		
		System.out.println("power(" + base + "," + exponent + ") is: " + power);
	}
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala