Java Math Class:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
In Java, all mathematical functions are the static methods of the Math class in the package java.lang. Important mathematical methods of the Java math class are given below in the table.
Math.abs() | Returns an absolute value of the given numeric data by ignoring the sign. |
Math.min() | Returns the smaller value. |
Math.max() | Returns the larger value. |
Math.sqrt() | Returns the square root of the value. |
Math.cbrt() | Returns the cube root of the value. |
Math.pow() | Returns the value of the first argument raised to the power of the second argument. |
Math.ceil() | Returns the smallest integer value greater than the given argument. |
Math.floor() | Returns the largest integer value smaller than the given argument. |
Math.round() | Returns the nearest integer value. |
Math.random() | Return any value between 0 and 1. |
Math.log() | Returns the natural logarithm of the value. |
Math.rint() | Converts double value to integral value in double format. |
Math.hypot() | Precision calculation of the sqrt() of x2+y2 |
Code:

Output:

Java Math Class Source Code:
package com.java_tutorial;
public class Math_class {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a = 20;
double b = 3;
double c = -30.5;
double d = 41.5;
double e = Math.random();
System.out.println("Absolute Value of a is: " + Math.abs(a));
System.out.println("Absolute Value of c is: " + Math.abs(c));
System.out.println("Minimum number of a and b is: " + Math.min(a, b));
System.out.println("Maximum number of a and b is: " + Math.max(a, b));
System.out.println("Square root of a is: " + Math.sqrt(a));
System.out.println("Square root of b is: " + Math.sqrt(b));
System.out.println("Cube root of a is: " + Math.cbrt(a));
System.out.println("Cube root of b is: " + Math.cbrt(b));
System.out.println("Power of a and b is: " + Math.pow(a, b));
System.out.println("Ceil Value of d is: " + Math.ceil(d));
System.out.println("Floor Value of d is: " + Math.floor(d));
System.out.println("Round Value of d is: " + Math.round(d));
System.out.println("Random Value of e is: " + e);
System.out.println("Logarithm of a is: " + Math.log(a));
System.out.println("Logarithm of b is: " + Math.log(b));
System.out.println("Rint function gives value of d is: " + Math.rint(d));
System.out.println("Using a and b Value of Hypotenuse is: " + Math.hypot(a, b));
}
}
Comments (No)