Coders Packet

How to calculate power of a number in minimum time complexity through recursion using JAVA

By Kartik Rakesh Khandelwal

In this tutorial, we have to calculate the power of a number with minimum time complexity through recursion using the JAVA programming language.

The purpose of this project is to calculate the power of a given number with minimum time complexity through recursion in a JAVA programming language.

Recursion: Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

Exponentiation is a mathematical operation, written as bn, involving two numbers, the base b, and the exponent or power n, and pronounced as "b raised to the power of n".[1][2] When n is a positive integer, exponentiation corresponds to repeated multiplication of the base: that is, bn is the product of multiplying n bases:

The below code is taking base and exponent as input from the user and then sending it as a parameter through calculatePower(num1, num2) function, calculating power through recursion, and returning the result.

import java.util.*;
public class fastestPowerCalculator {
  static int calculatePower(int a, int b)
      return 1;
      return calculatePower(a*a,b/2);
      return a*calculatePower(a,b-1);

  public static void main(String[] args) 
      Scanner sc = new Scanner(;
      System.out.println("Enter base:");
      int num1 = sc.nextInt();
      System.out.println("Enter exponent:");
      int num2 =sc.nextInt();

      int result = calculatePower(num1 , num2);
      System.out.println("Result is "+result);

Fastest Power Calculator

Download Complete Code


No comments yet