Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Power Function in Java?
2.1.
Signature
2.2.
Syntax
3.
How Do I Use the power Function in Java?
4.
Example of Power function in Java
5.
Some Special Cases of Power Function in Java
5.1.
Second Argument as Zero
5.2.
Second Argument as NaN
5.3.
Second Argument as Negative
6.
How to Return Integer type Value Using the Power Function
7.
Frequently Asked Questions
7.1.
How do you write 2 power n in Java?
7.2.
What does Math POW () return?
7.3.
What is Math pow with 3 arguments?
7.4.
Is there any alternative to the power function in Java?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Power Function in Java

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

Understanding the Math.pow() function in Java can be challenging for many developers. However, fear not – we're here to assist you in navigating the intricacies of this function, which can be likened to a robust calculator, simplifying complex mathematical operations and offering a versatile tool for various tasks.

power function in java

In many languages, the inbuilt power function is not so powerful, but it can handle several edge cases without failing in Java. Let's discuss them in detail.

What is Power Function in Java?

pow() function in Java is used to raise one number to the power of another. It takes two arguments: the base and the exponent, and returns the result as a double, with the syntax double result = Math. pow(base, exponent. 

The power function in Java returns the value of the first argument raised to the power of the second argument. It takes two arguments as parameters, and by default, it returns data type double. Unlike other programming languages, the power function in Java works almost like actual maths operations as it is not just limited to finding exponents. They can handle negative and decimal numbers as well.  

Signature

public static double pow(double base, double exponent)


Syntax

Math.pow(a,b) 

The syntax Math.pow(a, b) refers to a function where a and b are arguments. a is the base number, and b is the exponent. The function calculates and returns the value of a raised to the power of b. For example, if a is 2 and b is 3, it calculates 

Explanation

The above function takes two arguments, usually of double data type. The above function returns base^exponent as output. Here a is the base, and b is the exponent. Its time complexity is O(log b), and space complexity is O(1).

Also see, Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How Do I Use the power Function in Java?


In Java, the power function is used through `Math.pow(double a, double b)`, where `a` is the base and `b` is the exponent. It returns `a` raised to the power of `b`. For example, `Math.pow(2, 3)` returns 8.0, as it computes (23).

Example of Power function in Java

In the below example, we have shown some different combinations of base and exponents for better understanding.

Code

public class NinjaPower {
    public static void main(String[] args) {
        
        // Case 1
        double base = 2;
        double exponent = 3;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
        
        // Case 2
        base = 2;
        exponent = 0.5;
        result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
        
        // Case 3
        base = 0.2;
        exponent = 3;
        result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}


Output

output for power function

Some Special Cases of Power Function in Java

There are some peculiar cases when the argument has different values. Let’s discuss them:

Second Argument as Zero

If the second argument is a positive or negative zero, then the power function returns 1.0.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = -0;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}


Output

output for zero

Second Argument as NaN

If the second argument is not a number (NaN), then the power function also returns NaN.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = Double.NaN;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}


Output

output for NaN

Second Argument as Negative

If the second argument is a negative number, then the power function solves it just like general maths, i.e., a^(-x) becomes (1/a)^x.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = -2;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}

 

Output

output for negative argument

How to Return Integer type Value Using the Power Function

To return an integer value using the power function in Java, first use `Math.pow()` to compute the power, which returns a double, and then cast the result to an integer. For example, `(int) Math.pow(2, 3)` calculates (23) as 8.0 and then casts it to 8, an integer.

Example :

public class Main {
   public static void main(String[] args) {
       int base = 2;
       int exponent = 3;
       int result = (int) Math.pow(base, exponent); // Calculates 2^3 and casts the result to an integer
       System.out.println("Result: " + result); // Outputs: Result: 8
   }
}

In this code, Math.pow(2, 3) computes 2323 and returns 8.0 as a double. The (int) before Math.pow() casts this double to an integer, making result equal to 8.

Frequently Asked Questions

How do you write 2 power n in Java?

In Java, to write 2 raised to the power of n, you use `Math.pow(2, n)`. This function takes 2 as the base and n as the exponent, computing and returning (2n). For example, `Math.pow(2, 3)` returns 8.0.

What does Math POW () return?

The `Math.pow()` function in Java returns a double value representing the result of raising the first argument (base) to the power of the second argument (exponent). For example, `Math.pow(2, 3)` returns 8.0, which is (23).

What is Math pow with 3 arguments?

The Python `pow()` function with three arguments, `pow(base, exponent, modulus)`, computes the power and then applies the modulus, returning the remainder of the power divided by the modulus. This is useful for modular exponentiation, especially in cryptography.

Is there any alternative to the power function in Java?

We can use the exponentiation operator (**) or implement the power function using a loop. The Math.pow() method is generally the simplest and most efficient way to perform power functions in Java.

Conclusion

In this article, we discussed the Power function in Java. We also saw different examples and some peculiar cases and their output. The power function is provided in almost all major programming languages, but they should be coded correctly, and the edge cases should be kept in mind.

If you liked this, read our other related blogs:

Refer to our Guided Path to enhance your skills in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas!

But suppose you are just a beginner and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

Previous article
Java Math.round() method with Examples
Next article
Math pow
Live masterclass