In this tutorial, we will learn how to find the finding power of a number in java. The power of a number is defined as the value which is obtained by multiplying the base value n number of times where n is the exponent value.

The finding **power of a number in java** is used to calculate the value for the first integer value raised to the value of the second integer. There are some points we have to be careful about while using the pow() function.

- If the second integer is 0, the output will be always 1.
- When the value of the second integer is 1, the output will always be the first integer.
- If the second integer is NaN, the output will also be NaN.

So the main question were that how we can calculate the power of a number in java,how we can find the power of a number in java.

Lets go deep into that……………….

## Different Ways to calculate the power of a number

- We can calculate power with the help of a while loop.
- By using for loop.
- By using pow() function.

### Program to find power of a number in Java using While Loop

### Algorithm :

- Start
- Create an instance of the Scanner class.
- Declare two variables for the base and exponent.
- Ask the user to initialize both variables.
- Use a while loop to calculate the power of a number.
- Print the calculated value.
- Stop

### Code :

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base value: ");
int base = sc.nextInt();
System.out.println("Enter the exponent value: ");
int exp = sc.nextInt();
long result = 1;
System.out.print(base+ " raised to the power "+ exp+" is: ");
while (exp != 0)
{
result *= base;
--exp;
}
System.out.println(result);
}
```

### Output :

```
Enter the base value: 3
Enter the exponent value: 3
3 raised to the power 3 is: 27
```

### Program to find power of a number in Java using For Loop

### Algorithm :

- Start
- Create an instance of the Scanner class.
- Declare two variables for the base and exponent.
- Ask the user to initialize both variables.
- Use a for loop to calculate the power of a number.
- Print the calculated value.
- Stop

### Code :

```
public class CalculatePower {
public static void main(String[] args) {
int base = 5, exponent = 4;
long result = 1;
System.out.println("Value of base: " + base);
System.out.println("Value of exponent: " + exponent);
for (; exponent != 0; --exponent) {
result *= base;
}
System.out.println("Output: " + result);
}
}
```

### Output :

```
Value of base: 5
Value of exponent: 4
Output: 625
```

### Program to find power of a number in Java using pow() function

### Algorithm :

- Start
- Create an instance of the Scanner class.
- Declare two variables.
- Ask the user to initialize the variables.
- Use Math.pow() to calculate the power of the number.
- Print the value of the power of the number.
- Stop

### Code :

```
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base value: ");
int base = sc.nextInt();
System.out.println("Enter the exponent value: ");
int exp = sc.nextInt();
System.out.print(base+ " raised to the power "+ exp+" is: ");
double result = Math.pow(base, exp);
System.out.println(result);
}
}
```

### Output :

```
Enter the base value: 8
Enter the exponent value: 2
8 raised to the power 2 is: 64.0
```

This is the most optimized code in java with the least complexity. The pow() function is found under java.lang.Math is directly used to calculate the power of a number. The pow() function will directly take two numbers as integers and returns the output as the first integer value raised to the value of the second integer.

*Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.*