java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to 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 com.javacoderanch.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