Write a Java program to Find the cube root of a number using library method


  • import java.util.*; : This line imports the java.util package, which contains the Scanner class that we'll be using later in the code.
  • public class Cube_Root: This line declares a new public class called Cube_Root. The class name should match the file name (Cube_Root.java).
  • public static void main(String[] args) : This line is the main method that gets executed when the program is run. It takes an array of strings as an argument, which we won't be using in this program.
  • Scanner input = new Scanner(System.in); : This line creates a new Scanner object called input, which we'll use to get input from the user.
  • double num = 0; : This line declares a variable called num and initializes it to zero.
  • System.out.print("Enter The Number : ");: This line prints a message asking the user to enter a number.
  • num = input.nextDouble();: This line uses the nextDouble() method of the Scanner class to read a double value from the user and assign it to the num variable.
  • System.out.print("Cube Root of Number is : " + Math.cbrt(num)); : This line uses the Math.cbrt() method to find the cube root of the number entered by the user, and then prints the result to the console.
  • }: This line marks the end of the main method.
  • }: This line marks the end of the Cube_Root class.

Source Code

import java.util.*;
public class Cube_Root
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		double num = 0;
 
		System.out.print("Enter The Number : ");
		num = input.nextDouble();
 
		System.out.print("Cube Root of Number is : " + Math.cbrt(num));
	}
}

Output

Enter The Number : 12
Cube Root of Number is : 2.2894284851066637

Example Programs