Java Program to Calculate the Power of a Number

Java Program to Calculate the Power of a Number

What is Power of a Number ?

In mathematics, the power of a number refers to the number of times that number is multiplied by itself. It is represented by an exponent, which is a small number written to the right and above the base number. For example, if we have the base number 2 raised to the power of 3, it is written as 2³, which means 2 multiplied by itself 3 times:
2³ = 2 x 2 x 2 = 8

How to Calculate the Power of a Number ? 

To calculate the power of a number, you simply need to multiply the base number by itself the number of times indicated by the exponent. Here’s a general formula for calculating the power of a number:

a^n = a × a × a × … (n times)

where “a” is the base number and “n” is the exponent.

For example, let’s say we want to calculate 2 raised to the power of 4. Using the formula above, we get:

2^4 = 2 × 2 × 2 × 2 = 16

Similarly, if we want to calculate 5 raised to the power of 3, we get:

5^3 = 5 × 5 × 5 = 125

So, to calculate the power of a number, all you need to do is multiply the base number by itself the number of times indicated by the exponent.

Steps to calculate the Power of a Number : 

  1. Start by initializing the base number and exponent.
  2. Set a variable “result” to 1.
  3. Use a loop to multiply the base number by itself for the number of times indicated by the exponent. For example, if the exponent is 4, multiply the base number by itself four times.
  4. For each iteration of the loop, multiply the base number by the previous value of “result”.
  5. After the loop has finished, the value of “result” will be the answer to the exponentiation problem.
  6. Display the result to the user.

Pseudo Code for the above algorithm :

set base to the desired base number
set exponent to the desired exponent
set result to 1

for i from 1 to exponent do
  set result to result times base

display result

Ways to Find the power of a Number in Java :

Syntax :

double result = Math.pow(2, 4); // calculates 2 raised to the power of 4

Syntax :

int base = 2;
int exponent = 4;
int result = 1;

for(int i = 0; i < exponent; i++) {
    result *= base;
}

Syntax :

public static int power(int base, int exponent) {
    if(exponent == 0) {
        return 1;
    } else {
        return base * power(base, exponent - 1);
    }
}

// calling the power() method
int result = power(2, 4); // calculates 2 raised to the power of 4

Example 1 : 

Run
public class Main {
    public static void main(String[] args) {
        double result = Math.pow(2, 4); // calculates 2 raised to the power of 4
        System.out.println("2 raised to the power of 4 is: " + result);
    }
}

Output :

2 raised to the power of 4 is: 16.0

Example 2 : 

Run
public class Main {
    public static void main(String[] args) {
        int base = 2;
        int exponent = 4;
        int result = 1;

        for(int i = 0; i < exponent; i++) {
            result *= base;
        }

        System.out.println("2 raised to the power of 4 is: " + result);
    }
}

Output :

2 raised to the power of 4 is: 16

Example 3 : 

Run
public class Main {
    public static void main(String[] args) {
        int base = 2;
        int exponent = 4;
        int result = power(base, exponent);

        System.out.println("2 raised to the power of 4 is: " + result);
    }

    public static int power(int base, int exponent) {
        if(exponent == 0) {
            return 1;
        } else {
            return base * power(base, exponent - 1);
        }
    }
}

Output :

2 raised to the power of 4 is: 16

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription