Java Program to Calculate the Power of a Number

power of number

Java programs to calculate the power of a number have been shown here. The power of a number is denoted by $x^n$ where $x$ is the number and $n$ is the power. It signifies that $x$ is needed to be multiplied by itself $n$ times. In Java, the loops or inbuilt function like pow() can be used to calculate the power of a number. The recursive approach has also been shown. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to calculate the power of a number


1. Take a number x and the power n as input.

2. Multiply x with itself total n times and store the result inside a variable.

3. Display the result.




2. Pseudocode to calculate the power of a number


Input : A number $x$ and the power $n$

Output : $x^n$

1. Procedure power($x$, $n$):

2. $p \leftarrow 1$

3. Repeat for $i = 1$ to $n$

4. $p \leftarrow p * x$

5. Return $p$

6. End Procedure





3. Time complexity to calculate the power of a number


Time Complexity: O(n)

Where n is the power.





4. Program & output to calculate the power of a number




4.1. Java Program & output to calculate the power of a number using iteration

Code has been copied
/********************************
	 alphabetacoder.com
 Java program to calculate power 
 of a number using iteration
*********************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int num, power, result = 1, i;

        // take input of the number and power
        System.out.print("Enter the number = ");
        num = sc.nextInt();
        System.out.print("Enter the power = ");
        power = sc.nextInt();

        // calculate the value
        for (i = 1; i <= power; i++)
            result = result * num;

        // display result
        System.out.print("Result of " + num + "^" + power + " = " + result);
    }
}

Output


Enter the number = 8

Enter the power = 3

Result of 8^3 = 512





4.2. Java Program & output to calculate the power of a number using recursion

Code has been copied
/********************************
	 alphabetacoder.com
 Java program to calculate power 
 of a number using recursion
*********************************/

import java.util.Scanner;

class Main {
    // recursive function to calculate 
    // power of a number
    // here the number and power are integers
    public int calculate_power(int n, int power) {
        if (power < 1) // exit condition
            return 1;
        else
            return n * calculate_power(n, power - 1);
    }

    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of Main class
        Main obj = new Main();

        // declare variables
        int num, power;

        // take input of the number and power
        System.out.print("Enter the number = ");
        num = sc.nextInt();
        System.out.print("Enter the power = ");
        power = sc.nextInt();

        // display result by calling recursive function
        System.out.print("Result of " + num + "^" + power + " = " + obj.calculate_power(num, power));
    }
}

Output


Enter the number = 45

Enter the power = 4

Result of 45^4 = 4100625




4.3. Java Program & output to calculate power of a number using pow()

Code has been copied
/*********************************
	  alphabetacoder.com
 Java program to calculate power 
 of a number using pow() function
**********************************/

import java.util.Scanner;
import java.lang.Math;

class Main {
    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        double num, power, result;

        // take input of the number and power
        System.out.print("Enter the number = ");
        num = sc.nextDouble();
        System.out.print("Enter the power = ");
        power = sc.nextDouble();

        // calculate the value
        // using pow() function
        result = Math.pow(num, power);

        // display result
        System.out.print("Result of " + num + "^" + power + " = " + result);
    }
}

Output


Enter the number = 20

Enter the power = 1.75

Result of 20.0^1.75 = 189.14832180063516