Power Function in JavaThe 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 below points about the power function in Java:
Let's understand it with some examples: Example1: Calculate 5^2 using Java Power function PowerFunc1.java: Output: 25.0 Example2: Calculate 5^(-3) using power function PowerFunc2.java: Output: 0.008 Example3: Calculate -3^5 using the power function PowerFunc3.java: Output: -243.0 Example4: calculate 5^0 using the power function PowerFunc4.java: Output: 1.0 Example5: calculate 0.57^0.25 using the power function PowerFunc5.java: Output: 0.8688978326173192 From the above examples,
How to Return Integer type Value Using the Power FunctionWe 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
Next TopicPrimitive Data Types in Java
|