Java Program to Calculate Power of a Number
In Java, you can calculate a number raised to a power in two common ways: use Math.pow(), or multiply the base repeatedly with a loop. For example, 5 raised to the power 3 is 5 × 5 × 5 = 125.
Math.pow() is the simplest choice when you want Java’s built-in power function, including support for negative or fractional exponents. A loop is useful when the exponent is a non-negative integer and you want to see the repeated-multiplication logic directly.
Calculate Power of a Number using Math.pow()
The Math.pow() method takes two double values as arguments and returns the first argument raised to the power of the second argument. Its basic syntax is:
double result = Math.pow(base, exponent);
The Math class belongs to java.lang, so you do not need to add an import statement just to call Math.pow().
Java Program
/**
* Java Program - Power of a Number
*/
public class Power {
public static void main(String[] args) {
//numbers
int number = 5;
int power = 3;
double result = Math.pow(number, power);
System.out.println(number + " raised to the power " + power + " = " +result);
}
}
Output
5 raised to the power 3 = 125.0
Here, Math.pow(5, 3) evaluates 53. Because Math.pow() returns a double, the displayed result is 125.0 rather than 125.
Calculate Power of a Number using a Java For Loop
You can use Java For Loop to calculate the result of a number raised to the power of another number.
The loop starts with result = 1 and multiplies it by the base once for each value of the exponent. For 53, the values become 1 × 5 = 5, then 5 × 5 = 25, then 25 × 5 = 125.
Java Program
/**
* Java Program - Power of a Number
*/
public class Power {
public static void main(String[] args) {
//numbers
int number = 5;
int power = 3;
long result = 1;
for (int i = 0; i < power; i++)
result *= number;
System.out.println(number + " raised to the power " + power + " = " +result);
}
}
Output
5 raised to the power 3 = 125
This loop works directly for non-negative integer exponents. If power is 0, the loop does not run and result remains 1, which matches the usual rule that a non-zero number raised to the power 0 is 1.
Calculate 2 Raised to a Power in Java
To calculate powers of 2, pass 2 as the base. For example, 2 raised to the power 3 can be written with Math.pow(2, 3).
public class PowerOfTwo {
public static void main(String[] args) {
double result = Math.pow(2, 3);
System.out.println(result);
}
}
Output
8.0
Use Math.pow() for Negative or Decimal Exponents
The simple multiplication loop above is intended for non-negative integer exponents. When the exponent is negative or fractional, Math.pow() is usually the appropriate built-in method.
public class PowerExamples {
public static void main(String[] args) {
System.out.println(Math.pow(2, -3));
System.out.println(Math.pow(9, 0.5));
}
}
Output
0.125
3.0
In the first expression, 2-3 equals 1 / 23, which is 0.125. In the second expression, raising 9 to the power 0.5 gives its square root, 3.0.
Do Not Use the ^ Operator for Powers in Java
Java does not use ^ as an exponent operator. For integer operands, ^ performs bitwise XOR. Therefore, 2 ^ 3 does not mean 23.
public class PowerOperatorExample {
public static void main(String[] args) {
System.out.println(2 ^ 3);
System.out.println(Math.pow(2, 3));
}
}
Output
1
8.0
Math.pow() vs For Loop for Java Power Calculations
- Use
Math.pow()when you want a concise built-in solution or need negative or fractional exponents. - Use a for loop when the exponent is a non-negative integer and repeated multiplication is suitable for the problem.
- Remember that
Math.pow()returnsdouble, while an integer loop can keep an integer result as long as the chosen integer type does not overflow. - Do not use
^for exponentiation in Java.
Java Power Calculation Summary
In this Java Tutorial, we learned how to calculate the power of a number using Math library function or Java looping statements.
For most direct power calculations, Math.pow(base, exponent) is the simplest option. For non-negative integer exponents, a loop provides the same repeated-multiplication process explicitly.
TutorialKart.com