How to Do Exponents in Java

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
You can do exponents in Java.
Image Credit: scyther5/iStock/GettyImages

While there is no simple Java exponential operator, you can easily calculate an exponent using a few different methods. However, using power in Java (Math.pow) is the most flexible and straightforward approach. You need to import the java.lang.math class before you calculate the exponent, but this is a simple line of code, and the function itself has a syntax that is easy to use. The alternative methods use a for loop or a recursive call, but they only work for powers greater than or equal to 1, so you should try to use the first where possible.

Advertisement

Math.pow in Java

Video of the Day

The core of the method for calculating exponents in Java is the math.pow() function, which takes two values and calculates one to the power of the other. The syntax is "math.pow(base, exponent)" with the number you want raised to the power where it says "base" and the power you want it raised to where it says "power." You can make the result an integer by using (int) before the function, but without this, it automatically returns a "double" value and takes parameters as "double" too.

Advertisement

Video of the Day

Calculating the Exponent

The process of calculating a Java exponent basically involves only using this function. Open your Java IDE – Netbeans, for example. For the first line of the code, you import the class before performing the calculation, so type "import java.lang.math;" (without quotations) before moving on with the calculation. On the next line, type "class" and something descriptive after a space, such as "CalculatePower {" with the open bracket at the end to prepare for the remainder of the code.

Advertisement

On a new, indented line, write "public static void main( String args[] ) {" (again without quotation marks), and then move on with the calculation. Depending on the type you want to return – either integer values or double values, for example – type either "int" or "double" on the beginning of a new indented line. Then type something descriptive for the answer such as "ans," followed by "= (int) Math.pow(base, exponent)" noting that if you're using double values, type "(double)" instead of "(int)" after the equals sign.

Advertisement

Finally, type "System.out.println(+ans)" on another new line and then run the command to work out the exponent. As always, you type the numbers you want to use in place of "base" and "exponent." So if you're looking to calculate 52 as an integer, then your code should say:

Advertisement

Advertisement

import java.lang.Math;

public class CalculatePower {

public static void main( String args[] ) {

Advertisement

int ans = (int) Math.pow(5,2);

System.out.println(+ans);

}

}

Advertisement

Other Methods: For Loop

You can only use the remaining methods if the exponent you're raising the number to is 1 or greater. These involve using a for loop and a recursive call. Both of these methods are reasonably longwinded, but you can copy some existing code rather than having to write it all yourself. Using the for loop method, copy the following code into your Java IDE:

Advertisement

package exponent_example;

Advertisement

public class Exponent_example {

public static void main(String[] args) {

Advertisement

double num = 2;

int exp = 3;

double answer = Pow(num, exp);

System.out.println(answer);

Advertisement

}

public static double Pow(double num, int exp){

double result =1;

for (int i = 0; i < exp; i++) {

result *= num;

}

return result;

Advertisement

}}

The "num = 2" and "exp = 3" parts are where you enter the base number and the power you raise it to, respectively. Note that the exponent has to be an integer (hence the "int" before it on the line), which is another limitation of this method (although one that won't come up too often). To use this code, change those numbers to whatever you want to calculate and run it.

Other Methods: Recursive Call

The final approach you can use to calculate exponents in Java is a recursive call. Like the for loop method, you can only do this for exponents greater than or equal to 1 and for integer values. Copy the following code and use it in the same way:

package exponent_example;

public class Exponent_example {

public static void main(String[] args) {

double num = 3;

Advertisement

int exp = 2;

double answer = Pow(num, exp);

System.out.println(answer);

}

public static double Pow(double num, double exp) {

if (exp <= 0)

return 1;

return num * Pow(num, exp - 1);

}

}

Advertisement

Advertisement

references & resources