Power of Number in Java

Power of Number in Java Program. The power (or exponent) of a number indicates how many times it should be multiplied. It’s printed above and to the right of the basic number as a tiny number. In this case, the small “2” indicates that 8 should be multiplied twice: 82 = 8*8 = 64.

class Main
{
public static void main(String[] args)
{
int base = 5, exponent = 3;
long result = 1;
while (exponent != 0)
{
result *= base;
--exponent;
}
System.out.println("Answer = " + result);
}
}

 

Output:

Answer = 125

What is Long int in Java

A 64-bit two’s complement integer is the long data type. The signed long has a value of -263 at its lowest point and 263-1 at its highest point. You can express an unsigned 64-bit long with a minimum value of 0 and a maximum value of 264-1 with the long data type in Java SE 8 and later.

What is While Loop and How to Execute for Power of Number in Java

The while loop in Java is a control flow statement that allows code to be repeated based on a Boolean condition using this method we can calculate power of number in java. The while loop is similar to a looping if statement. If the condition evaluates to true, the body of the loop will be executed, and we will proceed to the update expression.

Power of Number in Java Program for Calculation

In Java, the power function is used to calculate a number that has been raised to the power of another number. This method takes two parameters and returns the first parameter’s value multiplied by the second parameter’s value.

An exponent is a number or letter that is written above and to the right of the base in a mathematical statement. It denotes that the base will be raised to a given level of strength. The base is x, while the exponent or power is n to calculate power of number in java.

Similar Programs