Integer.pow java

What is Integer.pow in Java?

Integer.pow is a built-in method in Java that calculates the power of an integer. It takes two arguments, the base, and the exponent, and returns the result of raising the base to the exponent.

Syntax:


int result = Integer.pow(base, exponent);

The base argument represents the base number, and the exponent argument represents the exponent of the power.

Example:

Let's say we want to calculate 2 raised to the power of 3:


int result = Integer.pow(2, 3);
System.out.println(result); // Output: 8

The output will be 8, which is the result of 2 raised to the power of 3.

Alternative way:

Another way to calculate the power of an integer is by using the Math.pow() method. This method can be used to calculate the power of any number, not just integers.

Syntax:


double result = Math.pow(base, exponent);

Here, the base and exponent arguments are both of type double.

Example:

Let's say we want to calculate 2 raised to the power of 3:


double result = Math.pow(2.0, 3.0);
System.out.println(result); // Output: 8.0

The output will be 8.0, which is the result of 2 raised to the power of 3.

It's important to note that the Math.pow() method returns a double value, while the Integer.pow() method returns an integer value.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe