Java

How to do Exponents in Java

Java offers multiple ways to calculate an exponent for example the Math.pow() method, for-loop, and while loop. All these approaches can be used to find the power of a number however, it is preferred to use the pow() method for calculating the exponents . Because the Math.pow() method can calculate the power of any value i.e. positive, or negative. Moreover, the Math.pow() method saves a lot of time as there is no need to define any user-defined logic.

This post will explain how to do exponents using the below-listed approaches:

So, let’s get started with the practical implementation of the above-given approaches.

How to Get an Exponent Using Math.pow() in Java?

Built-in methods always come up with ease, and so does the Math.pow() method. It is a predefined Java method of Math class that takes two numeric values as an argument. The Math.pow() method returns a value which is a result of first number raised to the power of the second number.

We’ll take a look at the below-given snippet to understand the basic syntax of the Math.pow() method in Java:

Math.pow(double base, double exponent);

Here in the above snippet, “Math” is a built-in Java class. The pow() is a predefined method of Math class. While the base and exponent are two parameters of the pow() method.

Now without wasting any time let’s directly jump into the practical implementation of the Math.pow() method in Java.

Example: How to do exponents in Java using the Math.pow() method

double num1 = 5;
double num2 = 3;
System.out.println("Result: " + Math.pow(num1, num2));

In this coding example, the Math.pow() method accepted two numeric values i.e. 5 and 3. Consequently, it will return the result as 5 raised to the power 3:

The resultant output shows the appropriateness of the Math.pow() method in Java.

How to Find the Exponent Using the While Loop in Java?

We can utilize the while loop to find the exponent in Java. Let’s take a look at the below-given snippet to learn how to find the exponent using the while loop in Java:

double base = 3;
double power = 7;
int count = 1;
while (power != 0) {
count *= base;
power--;
}
System.out.println("Result: " + count);

In this example program, we utilized the while loop to find the exponent. To do that, we specified a condition within the while loop i.e. “power != 0”. Within the while loop, we multiplied the “count” with the “base” value, which will keep multiplying until the condition remains true. The value of “power” will be decremented by 1 in each iteration, and this process will continue until the value of “power” becomes 0.

The output clarified that the while loop provides the appropriate results. Similarly, Java for-loop can be used to calculate the power of a number.

How to Get the Power of Negative Numbers in Java?

The Math.pow() method is equally effective for negative numbers as for positive numbers. This means we can utilize the Math.pow() method to find the power of a negative value. So, let’s do it!

double num1 = -5;
double num2 = 5;
System.out.println("Result: " + Math.pow(num1, num2));

In this example we utilized the pow() method to find the power of a negative value. The above piece of code will produce the below-given output:

The output shows that the Math.pow() method works fine on the negative values.

Conclusion

In Java, an exponent can be calculated using the built-in Java method named Math.pow(), for-loop, and while loop. However, it is preferred to use the pow() method for calculating the exponents because it can calculate the power of any value i.e. positive, or negative. This post considered some examples to explain how to calculate the power in Java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.