Calculate Power of a Number Using a While Loop in Java

Calculate Power of a Number Using a While Loop in Java. 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.

Long int in Power of a Number 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.

Without decimals, integer types store whole numbers, positive or negative. Byte, short, int, and long are all valid types. 

The numeric value determines which type you should use. From -9223372036854775808 to 9223372036854775807, a long can store a value. To be clear, at least 18 digits are required.

While Loop 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. Calculate Power of a Number Using a While Loop in Java for beginners to understand the looping concepts.

The condition is evaluated first in a while loop, and if it returns true, the statements inside the while loop are executed. This process is repeated until the condition returns false. 

When the condition returns false, the control exits the while loop and moves on to the next statement in the program.

When using a while loop, it’s important to remember that we need to use an increment or decrement statement inside it so that the loop variable changes with each iteration and the condition returns false at some point. 

We can now end the while loop’s execution; otherwise, the loop would continue indefinitely.

Calculate Power of a Number Using a While Loop in Java

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 a Number in Java.

Pre Decrement in Java

Before assigning a value to a variable, the pre decrement operator is used to decrement the value by one. 

After assigning a value to a variable, the post decrement operator is used to decrement the value by one.

The increment operator reduces a value by one. The value is incremented by one before being assigned to the variable with pre-increment I After assigning a value to a variable, the value is incremented using the post-increment (i++) method. 

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