Java Program - Cube Root of a Number
If a number is multiplied by itself two times (n*n*n), the final number will be the cube of that number and finding the cube root of a number is inverse operation of cubing the number. If x is the cube root of y, it can be expressed as below:
Alternatively, it can also be expressed as:
x3 = y
Method 1: Using cbrt() method of Java Math Class
The cbrt() method of Java Math class can be used to return cube root of a number.
import java.lang.Math; public class MyClass { public static void main(String[] args) { double x = 64; double y = 125; //cbrt() takes double datatype as argument double x1 = Math.cbrt(x); double y1 = Math.cbrt(y); System.out.println("Cube root of " + x + " is " + x1); System.out.println("Cube root of " + y + " is " + y1); } }
The above code will give the following output:
Cube root of 16.0 is 4.0 Cube root of 25.0 is 5.0
Method 2: Using pow() method of Java Math Class
The pow() method of Java Math class can also be used to calculate cube root of a number.
import java.lang.Math; public class MyClass { public static void main(String[] args) { double x = 64; double y = 125; //pow() takes double datatype as argument double x1 = Math.pow(x, 1/3.); double y1 = Math.pow(y, 1/3.); System.out.println("Cube root of " + x + " is " + x1); System.out.println("Cube root of " + y + " is " + y1); } }
The above code will give the following output:
Cube root of 64.0 is 3.9999999999999996 Cube root of 125.0 is 4.999999999999999
Recommended Pages
- Java Program - To Check Prime Number
- Java Program - Bubble Sort
- Java Program - Selection Sort
- Java Program - Maximum Subarray Sum
- Java Program - Reverse digits of a given Integer
- Java - Swap two numbers
- Java Program - Fibonacci Sequence
- Java Program - Insertion Sort
- Java Program - Find Factorial of a Number
- Java Program - Find HCF of Two Numbers
- Java Program - To Check Whether a Number is Palindrome or Not
- Java Program - To Check Whether a String is Palindrome or Not
- Java Program - Heap Sort
- Java Program - Quick Sort
- Java - Swap Two Numbers without using Temporary Variable
- Java Program - To Check Armstrong Number
- Java Program - Counting Sort
- Java Program - Radix Sort
- Java Program - Find Largest Number among Three Numbers
- Java Program - Print Floyd's Triangle