Introduction to Math pow Java, in this tutorial you will learn how to use math pow in Java?

java.lang.Math.pow() in java is a very commonly used method. It takes in 2 parameters a and b and returns a raised to power b.

**Syntax of java math pow method**

`Math.pow(a,b)`

#### Example 1 : Math pow java code

For instance, here’s a java code to take two numbers a and b, and print a raised to power b.

```
public class MathPow {
public static void main(String[] args) {
int a = 2;
int b = 2;
double ans = Math.pow(a,b);
System.out.println(a+" raised to power "+b+" is "+ans);
}
}
```

**Output**

`2 raised to power 2 is 4.0`

That’s it.

A simple method with lots of uses.

Available at java.lang package, the Math class has more methods to help in mathematical and arithmetical operations. Math.floor(), Math.round(), Math.ceil(), Math.acos(), Math.asin(), many more.

That’s a story for another day, now let’s analyse another interesting example of `Math.pow()`

.

#### Example 2

Now, try to guess the output of the following code snippet.

```
public class MathPow {
public static void main(String[] args) {
char a = 'd';
int b = 2;
double ans = Math.pow(a,b);
System.out.println(a+" raised to power "+b+" is "+ans);
}
}
```

Well, the answer lies in the fact that the ASCII value for (a-z) is (97-122).

Similarly, (A-Z) is (65-90). So, probably you have guessed already that the answer will be the ASCII value of ‘d’ raised to power 2.

However, it is quite unusual for one to use characters in this scenario.

Still, it gives an understanding that java can cast characters to an integer with their ASCII values automatically.

#### Example 3

In the shell, what we are doing here with `Math.pow()`

is called Exponentiation.

It is a Mathematical operation involving 2 numbers- base b and exponent e. (b^e) Read as b “raised to power” e.

Basically, we multiply the base with itself ‘e’ times. But if e is 0, no matter what is the value of the base, the result will be 1.** Anything raised to power 0 is 1**.

`Math.pow(2,0)`

**Value**

`1.0`

#### Example 4

Now let’s try to calculate 2^(2^2). The answer should be 16. (2^4).

Here’s the code for help.

```
public class MathPow {
public static void main(String[] args) {
int a = 2;
int b = 2;
int c = 2;
double ans = Math.pow(a,Math.pow(b,c));
System.out.println(a+" raised to power "+b+" raised to power "+c+" is "+ans);
}
}
```

Output

`2 raised to power 2 raised to power 2 is 16.0`

## Math.pow() java Exercise

If you have come so far, here’s an exercise for you. Try to create a method called power(base,exp), and return base raised to power exp.

If implemented properly it should behave the same as the `Math.pow()`

method.

Great if you tried! Now let’s look at the solution.

```
public class MathPow {
static double power(int base, int exp) { // returns base^exp
if(exp == 0) { //zero exponent rule
return 1;
}
else if(exp > 0) { //for positive power
return base*power(base, exp-1);
}
else if(exp < 0) { //for negative power
return (1.0/base)*power(base, exp+1);
}
return 0; // no use but need to mention to avoid a compilation error.
}
public static void main(String[] args) {
int a = 2; // base
int b = -2; // exponent
double result = power(a,b);
System.out.printf("%d raised to power %d is %.2f ",a,b,result);
/***simple if-else code to compare with java.lang.Math.pow()***/
if(power(a,b) == Math.pow(a, b))
System.out.println("***Correct Answer***");
else
System.out.println("***Wrong Answer***");
}
}
```

We have used simple recursion to create the `power()`

method.

First, we check **if the exponent is 0**. If so, we return 1.

Otherwise, we check **if the exponent is greater than 0**. If so, we make a recursive call to power() method`return base*power(base, exp-1);`

with exp value **reduced** by 1 and multiply the base with it.

Else, **the exponent must be negative**. Therefore you can replace `else if(exp < 0) {`

with `else`

and the code will work same.

In this case, we have to return `(1.0/base)*power(base, exp+1)`

. The **‘1.0’** is important! If replaced by ‘1’ the expression will be treated as an integer and the value will likely become 0.

Since the exponent is negative be sure to **increase** it by 1 in the recursive call.

Lastly, we have compared the results with that of `Math.pow()`

. If same, we print “Correct”, “Wrong” if not.

**Output**

`2 raised to power -2 is 0.25 ***Correct Answer***`

You can check out How to do Exponents in Java?

**Like this article?** Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed.

Easy to understand and really liked this article on math pow java

Thank you, Jackson! Happy to hear that.

How can I learn other concept of Java ? Do you provide live sessions for it?

Hi Dimick,

Thank you for reaching out to us. You can learn the concept of Java with our expert as we do give lives sessions. If in case you want to know more details.

Please feel free to reach us out on help@letstacle.com.