Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaCalculating the Power of a Number in Java Without Using Math pow()...

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

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 :

  1. Calculating the power of a number through while loop or for loop.
  2. 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 loop
import 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);
    }
}


Output

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 loop
import 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);
    }
}


Output

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 Conquer
class 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));
    }
}


Output

8

 Time Complexity: O(log N), where N is power.

Auxiliary Space: O(1) as it is using constant variables

RELATED ARTICLES

Most Popular

Recent Comments