# 1. Introduction

In this blog post, we will write a Java program to calculate the power of a number. This operation is represented as *base^exponent*. For instance, 2^3 would be 2 multiplied by itself twice, which equals 8.

# 2. Program Steps

1. Import the *Scanner* class from the *java.util* package for user input.

2. Define the main class named *PowerCalculator*.

3. Inside the main class, define the *main* method.

4. Inside the *main* method, create an object of the *Scanner* class for user input.

5. Prompt the user to enter the base and the exponent.

6. Calculate the result by using the *Math.pow* method.

7. Print the calculated power of the number.

# 3. Code Program

```
import java.util.Scanner; // 1. Importing Scanner for user input
public class PowerCalculator { // 2. Defining the main class
public static void main(String[] args) { // 3. Defining the main method
Scanner input = new Scanner(System.in); // 4. Creating Scanner object for user input
System.out.print("Enter the base: "); // 5. Prompting user to enter the base
double base = input.nextDouble(); // Receiving the base
System.out.print("Enter the exponent: "); // 5. Prompting user to enter the exponent
double exponent = input.nextDouble(); // Receiving the exponent
double result = Math.pow(base, exponent); // 6. Calculating power using Math.pow method
System.out.println(base + " raised to the power of " + exponent + " is: " + result); // 7. Printing the result
input.close(); // Closing the Scanner object to avoid memory leak
}
}
```

### Output:

Enter the base: 3 Enter the exponent: 4 3.0 raised to the power of 4.0 is: 81.0

# 4. Step By Step Explanation

– Step 1: The *Scanner* class from the *java.util* package is imported to facilitate user input.

– Step 2: The main class named *PowerCalculator* is defined.

– Step 3: The *main* method, which is the entry point of the program, is defined inside the main class.

– Step 4: A *Scanner* object named "input" is created to take user input.

– Step 5: The user is prompted to enter the *base* and the *exponent*. For this example, the user enters *3* for the base and *4* for the exponent.

– Step 6: The program then calculates the result by using the *Math.pow* method, which takes the base and the exponent as parameters and returns the power of the number.

– Step 7: Finally, the program prints out the result, showing that *3.0* raised to the power of *4.0* is *81.0*.

– Lastly, the *Scanner* object is closed to avoid any potential memory leak.