Calculating the Power of a Number in Java Without Using Math pow() Method - GeeksforGeeks

03 Mar, 2021
We need to calculate a number raised to the power of some other number without using the predefined function java.lang.Math.pow()
In Java, we can calculate the power of any number by :
Calculating the power of a number through while loop or for loop.Calculating the power of a number by the divide and conquer method.
Calculating the power of a number through while loop or for loop.
Calculating the power of a number by the divide and conquer method.
To calculate the power of any number, the base number and an exponent are required. 
Prerequisite:  
The basic understanding of Java arithmetic operators, data types, basic input/output and loop etc.
Example:
Input : base = 3, exponent = 3
Output: 27

Input : base = 2, exponent = 4
Output: 16
Implementation:
1. Using while loop: The base and power values have been assigned respective values. Now, the while Loop will keep multiplying result variable by base variable until the power becomes zero.
Below is the implementation of the problem statement using the while loop: 
Java
// Calculate the power of a number in Java using while loopimport java.io.*;  public class JavaApplication1 {    public static void main(String[] args)    {        int base = 3, power = 3;        int result = 1;        // running loop while the power > 0        while (power != 0) {            result = result * base;            // power will get reduced after            // each multiplication            power--;        }        System.out.println("Result =  " + result);    }}
Result =  27

Time Complexity: O(N), where N is power.
2. Using for loop: The base and power values have been assigned respective values. Now, in the above program, we can use for loop instead of while loop.
Below is the implementation of the problem statement using the while loop: 
Java
// Calculate the power of a number in Java using for loopimport java.io.*;  public class JavaApplication1 {    public static void main(String[] args)    {        int base = 3, power = 3;        int result = 1;          for (power = 3; power != 0; power--) {            result = result * base;        }        System.out.println("Result =  " + result);    }}
Result =  27

Time Complexity: O(N), where N is power.
3. Using the Divide and conquer method: Above operation can be optimized to O(log N) by calculating power(base, exponent/2) only once and storing it.
Java
// Calculate the power of a number// in Java using Divide and Conquerclass GFG {      public static int power(int x, int y)    {        int temp;        if (y == 0)            return 1;        temp = power(x, y / 2);          if (y % 2 == 0)            return temp * temp;        else {            if (y > 0)                return x * temp * temp;            else                return (temp * temp) / x;        }    }      // Driver code    public static void main(String[] args)    {        int x = 2;        int y = 3;        System.out.println(power(x, y));    }}
8

 Time Complexity: O(log N), where N is power.
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java