# Write a Java Program to Calculate the Power of a Number

Calculating the power of a number is a common operation in mathematics and programming.

In Java, we can use the Math.pow() method to calculate the power of a number.

However, if you want to calculate the power of a number without using any built-in methods, you can use a simple algorithm.

To calculate the power of a number, you need to multiply the number by itself n times, where n is the power you want to calculate.

For example, to calculate the power of 2 to the 3rd power (2^3), you need to multiply 2 by itself 3 times (222), which equals 8.

Here is a Java program that calculates the power of a number using a simple algorithm:

```import java.util.Scanner;

public class PowerOfNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

System.out.print("Enter the power: ");
int power = scanner.nextInt();

int result = 1;

for(int i=1; i<=power; i++) {
result *= num;
}

System.out.println(num + " to the power of " + power + " is " + result);
}

}```

In this program, we first ask the user to enter a number and the power they want to calculate. We then initialize a variable called `result` to 1.

We use a for loop to multiply the number by itself n times, where n is the power entered by the user.

We do this by multiplying the `num` variable by itself and storing the result in the `result` variable.

We repeat this process for the number of times specified by the `power` variable.

Finally, we print out the result.

When you run this program and enter a number and a power, it will calculate the power of the number and print out the result.

In conclusion, calculating the power of a number is a simple algorithm that can be implemented in Java using a for loop.

You can also use the built-in Math.pow() method if you want a more efficient way of calculating the power of a number.

###### Editorial Team

Programming Cube website is a resource for you to find the best tutorials and articles on programming and coding.