Math Functions in Java


The Java Math class has many methods that allows you to perform mathematical tasks on numbers. The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square and root. The java.lang.Math contains a set of basic math functions for obtaining the absolute value, highest and lowest of two values, rounding of values.

Methods:

  • abs() : absolute value of return to the positive value
  • sqrt() : The square root of the argument
  • max() : Maximum of the two values passed in the argument
  • min() : Minimum of the two values passed in the argument
  • ceil() : Rounds float value up to an integer value
  • floor() : Rounds float value down to an integer value
  • pow() : Value of the first parameter raised to the second parameter

This is a Java program that demonstrates the use of various built-in math functions provided by the Math class in Java. The program starts with importing the Math class, which contains static methods for performing mathematical operations.

The program then calls various Math methods, such as Math.abs() to find the absolute value of a number, Math.round() to round a number to the nearest integer, Math.ceil() to find the smallest integer greater than or equal to a number, and Math.floor() to find the largest integer less than or equal to a number.

Next, the program demonstrates the use of other Math functions, such as Math.max() to find the maximum of two numbers, Math.sqrt() to find the square root of a number, Math.pow() to raise a number to a power, Math.log() to find the natural logarithm of a number, Math.log10() to find the base-10 logarithm of a number, and Math.sin(),Math.cos(), and Math.tan() to find the sine, cosine, and tangent of an angle, respectively.

Source Code

public class MathFunctions {
    public static void main(String[] args)
    {
        //Built-in Math Function in Java
        System.out.println("Absolute value : " +Math.abs(-45));
        System.out.println("Round value : " +Math.round(2.288));
        System.out.println("Ceil value : " +Math.ceil(2.588));
        System.out.println("Floor value : " +Math.floor(2.588));
 
        double a = 2;
        double b = 3;
        System.out.println("Maximum number of a and b is: " +Math.max(a, b));
        System.out.println("Square root of b is: " + Math.sqrt(b));
        System.out.println("Power of a and b is: " + Math.pow(a, b));
        System.out.println("Logarithm of a is: " + Math.log(a));
        System.out.println("log10 of a is: " + Math.log10(a));
        System.out.println("Sine value of a is: " +Math.sin(a));
        System.out.println("Cosine value of a is: " +Math.cos(a));
        System.out.println("Tangent value of a is: " +Math.tan(a));
 
    }
}
 

Output

Absolute value 			 : 45
Round value 			 : 2
Ceil value 			 : 3.0
Floor value			 : 2.0
Maximum number of a and b is     : 3.0
Square root of b is		 : 1.7320508075688772
Power of a and b is		 : 8.0
Logarithm of a is		 : 0.6931471805599453
log10 of a is			 : 0.3010299956639812
Sine value of a is		 : 0.9092974268256817
Cosine value of a is		 : -0.4161468365471424
Tangent value of a is		 : -2.185039863261519
To download raw file Click Here

Basic Programs