Java Program to calculate power of a number

In this program you'll learn, How to calculate power of a number

To understand this example, you should have the knowledge of the following Java programming topics:

Calculating power of a number

A java program to calculate the power of a number is as follows:

public class power {
    public static  void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Base Number: ");
        int baseNumber = input.nextInt();
        System.out.print("Power of : ");
        int powerOf = input.nextInt();
        int output = baseNumber;
        for(int i=1;i<powerOf;i++){
            output*=baseNumber;
        }
        System.out.println(baseNumber + " power of " + powerOf + " = " + output );
    }
}

Output:

Base Number: 2
Power of : 6
2 power of 6 = 64

Here we are taking two input from user and storing in a variable baseNumber, powerOf. To calculating the power of a number we initialize the output variable with base number and multiplying the baseNumber in a loop for powerOf-1 times inorder to get final result.