How do I raised a number to the power of n?

The static method Math.pow(double a, double b) can be use to raised the value specified in the a to the power of the value specified in b.

package org.kodejava.example.math;

public class PowerExample {

    public static void main(String[] args) {
        double cubeRoot = 5d;

        // Get the cubed number of cube root
        // x cubed = x^3 (multiplication three times)
        double cubed = Math.pow(cubeRoot, 3);
        System.out.println(cubeRoot + " cubed is " + cubed);

Our program print the following output:

5.0 cubed is 125.0


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.