Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java cbrt Function

by suresh

The Java cbrt Function is one of the Java Math Library functions, and it finds the cube root of a specified expression or an individual double value.

Java cbrt Function syntax

The syntax of the Math.cbrt in Java Programming language is as shown below. The following function accepts a positive double value as an argument. It returns the cube root of the specified expression or Value.

static double cbrt(double number); //Return Type is Double

// In order to use in program: 
Math.cbrt(double number);

Number: It can be a double value or a valid numerical expression to find the cube root.

  • If the number argument is positive or negative double value, Math.cbrt function will return the cube root of a given value.
  • If the number argument is not a number (NaN), java Math.cbrt function will return NaN.
  • Moreover, if it is infinity, Math.cbrt function will return the result as Infinity with the same sign.

Java cbrt Function Example

The Java Math.cbrt Function finds the cube root of a specified expression or an individual double value. In this Java program, We are going to find the cube root and display the output.

// Java Math.cbrt Function
package MathFunctions;

public class CbrtMethod {
	public static void main(String[] args) {
		double a = Math.cbrt(100.90 + 150.10 - 220.50 + 10.50);
		System.out.println("Cube Root results = " + a);	

		System.out.println("\nCube Root of Zero: " + Math.sqrt(0));
		
		System.out.println("\nCube Root of Positive Number: " + Math.cbrt(64));
		System.out.println("Cube Root of Positive Number: " + Math.cbrt(100.95));
		
		System.out.println("\nCube Root of Negative Number: " + Math.cbrt(-850.70));
		System.out.println("Cube Root of Negative Number: " + Math.cbrt(-125.00));
	}
}

OUTPUT

Java cbrt Function 1

ANALYSIS

First, We declared a variable of type Double and performed the Math.cbrt function directly on expression.

double a = Math.cbrt(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Cube Root results = " + a);

Next, We used the Math.cbrt Function directly on Positive double values.

System.out.println("\nCube Root of Positive Number: " + Math.cbrt(64));
System.out.println("Cube Root of Positive Number: " + Math.cbrt(100.95));

Next, We used the java Math.cbrt Function directly on Negative double values.

System.out.println("\nCube Root of Negative Number: " + Math.cbrt(-850.70));
System.out.println("Cube Root of Negative Number: " + Math.cbrt(-125.00));

Java cbrt on Array example

In this Java program, we find the cube roots of bulk data or array. Here, we are going to declare an array of double type and find the cube root of every element in an array.

package MathFunctions;

public class CbrtOnArrays {
	public static void main(String[] args) {
		
		double [] CbrtArray = {27.00, 64.00, 125.00, -500.90, -124.8799, 1234.8597};

		for (int i = 0; i < CbrtArray.length; i++) {
			System.out.format("Cube Root of Array Element = %.4f\n", Math.cbrt(CbrtArray[i]));
		}
	}
}

OUTPUT

Java cbrt Function 2

ANALYSIS

Within this Java cbrt function example, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check the condition (i < cbrtArray.length). As along the condition is True statement inside the for loop will be executed.

TIP: Java cbrtArray.length finds the length of an array.

for (int i = 0; i < CbrtArray.length; i++) {

Here, we used the cbrt Math function directly inside the System.out.format statement. It means the compiler calls the static double cbrt(double number) to find the cube root of corresponding values.

System.out.format("Cube Root of Array Element = %.4f\n", Math.cbrt(CbrtArray[i]));

NOTE: If you want to find the cube root of a single item, then use: Math.cbrt(myArray[index_position])

Java cbrt function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the cube root of list elements.

package MathFunctions;

import java.util.ArrayList;

public class CbrtMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> cbrtList = new ArrayList<Double>(5);
		cbrtList.add(12345.998);
		cbrtList.add(-180.05);
		cbrtList.add(1500.9876);
		cbrtList.add(1897.01);
		cbrtList.add(2596.98);
		cbrtList.add(-9878.1898);
		
		for (double x : cbrtList) {
			System.out.println("Cube Root of ArrayList =  " + Math.cbrt(x));
		}
	}
}

OUTPUT

Java cbrt Function 3

ANALYSIS

In this cbrt function example, we used the For Loop to iterate the double values in ArrayList

for (double x : cbrtList) {

The following statement calls the Math.cbrt method ( static double cbrt(double x) ) to find the cube root of corresponding values.

System.out.println("Cube Root of ArrayList =  " + Math.cbrt(x));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy