Cube root java – Java Program to Find Cube Root of a Number

Cube root java: In the previous article, we have seen Java Program to Convert an Angle in Degrees to Radians

In this article we are going to see how to find cube-root of any given number using Java programming language.

Java Program to Find Cube Root of a Number

Cube root in java: Before Jumping into the program directly let’s see how to find cube-root of any given number.

Explanation:

Java cube root: We can get the cube root of a number by finding out the prime factorization of the given number first. Where prime factorization refers to writing any number as the products of prime numbers.

After getting the prime factorization then applying the cube root formula. Suppose, ‘num‘ is any number such that,

num = n * n * n.

Example:

Given number “num” = 27

So the value of “num” can be written as 3*3*3

So 3√27 = 3

Let’s see different ways to find cube-root of any given number.

Method-1: Java Program to Find Cube Root of a Number By Using Math.cbrt() Method (Static Input)

In Java we have inbuilt method Math.cbrt() which can be used to find cube root of a number. Now we will use this inbuilt method to find out the cube root of a double value.

Approach: 

  • Declare a double variable say ‘num’ and assign the value to it, it is the value whose cube-root we will find out.
  • Now by using Math.cbrt() find the cube root of number.
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        //a number declared
        double num = 27;
        //finding cube root by using inbuilt method Math.cbrt()
        double cbrt = Math.cbrt(num);
        System.out.println("The cube root of  " + num + " is " + cbrt);
    }
}

Output:

The cube root of 27.0 is 3.0

Method-2: Java Program to Find Cube Root of a Number By Using Math.cbrt() Method (Dynamic Input)

Approach:

  • Declare a double variable say ‘num’ and take the value as user input, it is the value whose cube-root we will find out.
  • Now by using Math.cbrt() find the cube root of number.
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner s = new Scanner(System.in);                               
        System.out.println("Enter a number to find its cube-root: ");
        double num = s.nextDouble();                                           

        //finding cube root by using inbuilt method Math.cbrt()
        double cbrt = Math.cbrt(num);
        System.out.println("The cube root of  " + num + " is " + cbrt);
    }
}

Output:

Enter a number to find its cube-root: 
4
The cube root of 4.0 is 1.5874010519681996

Method-3:  Java Program to Find Cube Root of a Number By Using Math.Pow() Method

Approach:

  • Declare a double variable say ‘num’ and take the value as user input, it is the value whose cube-root we will find out.
  • Now by using Math.pow(num,1/3.) find the cube root of number.
  • Print the result.

Program:

import java.lang.Math;

public class Main
{
  public static void main(String[] args) 
  {
        //a number declared
        double num = 14;
        
        //finding cube root of number using Math.pow(num, 1/3.)
        //where number is the value whose cube root is to be found out
        double cbrt = Math.pow(num, 1/3.);
        System.out.println("Cube root of " + num + " is " + cbrt);
  }
}
Output:

Cube root of 14.0 is 2.4101422641752297

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: