Power Function in Java29 Mar 2025 | 5 min read The power function in Java is Math.pow(). It is used to get the power of the first argument to the second argument. It takes two arguments and returns the value of the first argument raised to the second argument. It returns a double type value. The pow() function takes place in java.lang.Math.pow () library. For example, to calculate the 5 to power 2, then it can be done as follows: Math.pow(5,2) =25 Syntax: where, a is base b is exponent The above syntax will return the value of a^b in double data type. Consider the following points about the power function in Java:
Let's understand it with some examples: Example 1: Calculate 5^2 Using Java Math.pow() Function PowerFunc1.java: Output: 25.0 Example 2: Calculate 5^(-3) Using Java Math.pow() Function PowerFunc2.java: Output: 0.008 Example 3: Calculate -3^5 Using Java Math.pow() Function PowerFunc3.java: Output: -243.0 Example 4: Calculate 5^0 Using Java Math.pow() Function PowerFunc4.java: Output: 1.0 Example 5: Calculate 0.57^0.25 Using Java Math.pow() Function PowerFunc5.java: Output: 0.8688978326173192 In the above examples,
How to Return Integer Type Value Using the Math.pow() Function?We can also return the integer type value using the power function. For this, we need to explicitly cast it to Integer. Consider the below example: PowerFunc6.java: Output: 25 Calculate Power of a Number Using While LoopIn Java, a while loop can effectively implement a power function, where a base is multiplied by itself until an exponent reaches zero. This method is straightforward and offers good control over the loop, making it ideal for beginners to practice both Java loops and simple mathematical operations. Filename: Output: 3 raised to the power of 4 is 81 Calculate Power of a Number Using do-while LoopAlgorithm:
File Name: PowerCalculator.java Output: 2 raised to the power of 3 is 8 Calculate the Power of a Number Using a for LoopCalculating the power of a number using a for loop in Java involves a straightforward approach where you repeatedly multiply a base number by itself for a number of times equal to the exponent. Here's how to do it, including a complete Java example: File Name: PowerCalculator.java Output: 2 raised to the power of 5 is 32 Calculate the power of a number using pow() functionUsing the Math.pow() function in Java is a straightforward and efficient way to calculate the power of a number. This built-in method is part of the Java Math class and is used for computing the value of one number raised to the power of another. Here's how you can use it: File Name: PowerCalculator.java Output: 2 raised to the power of 8 is 256 |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India