It’s very basic in Arithmetic to do the ‘power of a number’. What this means is that a number will be multiplied as many times as what the power is. In this post, we will learn how to power a number in Java using Math.pow()

Signature:

double java.lang.Math.pow(double a, double b)

Math pow() method in Java with example...!!! Share on X

Examples

import java.lang.Math;

public class Math_pow_example {

    public static void main(String args[]) {
        int intResult;
        double doubleResult;
        double nan = Double.NaN;

        //power argument is 4 on a integer
        //pow() by definition requires double as arguments. if you use integer, then you need to cast it like below. 
        intResult = (int) Math.pow(5, 4);
        System.out.println(intResult);

        //power argument is 8.2 on a double
        doubleResult = Math.pow(7.6, 8.2);
        System.out.println(doubleResult);

        //power argument is one 
        doubleResult = Math.pow(7, 1);
        System.out.println(doubleResult);

        //power argument is Nan 
        doubleResult = Math.pow(10, nan);
        System.out.println(doubleResult);

        //power argument is zero 
        doubleResult = Math.pow(55, 0);
        System.out.println(doubleResult);

    }
}

Output:

625
1.6698269141545841E7
7.0
NaN
1.0

I hope this article will help you in understanding how to get the power of a number in Java. 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts