Share & learn

Through Innovative Digital Library

What are different methods in Math class in Java?

Explain about different methods from Math class in Java.
 



Answers

Posted by Paul on April 27th 2018 12:08 PM

Math class in Java : For performing basic mathematical operations, Java has provided java.lang.Math class. This class contains the basic methods needed for performing basic numeric operations. e.g. of numeric operations that can be performed using java.lang.Math class : elementary exponential, logarithm, square root and trigonometric functions Class Declaration : Here is the declaration of Math class : public final class Math extends Object Methods of Math class : 1. abs() : This method will return the absolute values of passed arguments.

//Java program to implement abs() method

public class MathClassDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
int num1 54;
        
int num2 = -25;
        
double num3 34.6;
        
float num4 12.45f;

        
// calculate absolute values
        
System.out.println("Absolute value of " num1 " : " Math.abs(num1));
        
System.out.println("Absolute value of " num2 " : " Math.abs(num2));
        
System.out.println("Absolute value of " num3 " : " Math.abs(num3));
        
System.out.println("Absolute value of " num4 " : " Math.abs(num4));

    }

}

Posted by Paul on April 27th 2018 12:13 PM

2. ceil() : This method will return the smallest ( i.e closest to negative infinity ) value which is greater than or equal to the argument and is equal to a mathematical integer. 3. floor() : This method will return the largest ( i.e. closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer.

//Java program to implement ceil() and floor() method

public class MathClassDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        // calculate ceil and floor
        
double num5 22.34;
        
double num6 0.98;

        
System.out.println("\n\nCeil value of " num5 " : " Math.ceil(num5));
        
System.out.println("Floor value of " num5 " : " Math.floor(num5));
        
System.out.println("Ceil value of " num6 " : " Math.ceil(num6));
        
System.out.println("Floor value of " num6 " : " Math.floor(num6));

    }

}

Posted by Paul on April 27th 2018 12:17 PM

4. max() : This method will return the maximum number of two values. 5. min() : This method will return the minimum of two values.

public class MathClassDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
int num1 54;
        
int num2 = -25;

double num5 22.34;
double num6 0.98;


        
// find min and max
        
System.out.println("\n\nMinimum out of " num1 " and " num2 " : " Math.min(num1num2));
        
System.out.println("Maximum out of " num1 " and " num2 " : " Math.max(num1num2));
        
System.out.println("Minimum out of " num5 " and " num6 " : " Math.min(num5num6));
        
System.out.println("Maximum out of " num5 " and " num6 " : " Math.max(num5num6));

        
    }

}

Posted by Paul on April 27th 2018 12:22 PM

6, round() : round method is used to round off value.

public class MathClassDemo2 {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
double num1 34.4;
        
double num2 0.77;

        
// calculate round off value
        
System.out.println("Round off value " num1 " is : " Math.round(num1));
        
System.out.println("Round off value " num2 " is : " Math.round(num2));
        

    }

}

Posted by Paul on April 27th 2018 12:25 PM

7. exp() : This method will return the base of natural log (e) to the power of argument 8. Log() : This method will return the natural log of the argument 9. Pow() : This method will takes 2 arguments as input and returns the value of the first argument raised to the power of the second argument

public class MathClassDemo2 {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
double num1 34.6;
        
double num2 0.77;

        
// calculate exp, log and power
        
System.out.println("Round off value "+num1+" is : " Math.exp(num1));
        
System.out.println("log(" num2 ") = " Math.log(num2));
        
System.out.println("pow(2, 3) = " Math.pow(2.03.0));


    }

}

Posted by Paul on April 27th 2018 12:26 PM

10. Sqrt() : This method will return the square root of the argument 11. cbrt : This method will return the cube root of a passed value.

public class MathClassDemo {

    public static 
void main(String[] args) {
        
// calculate square root and cube root

        
int num7 64;
        
int num8 125;
        
System.out.println("\n\nSquare Root of " num7 " is : " Math.sqrt(num7));
        
System.out.println("Cube Root of " num8 " is : " Math.cbrt(num7));

    }

}
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
KJ2P

related questions

Most liked questions

Most answered questions

Login
KJ2P Refresh
Register
KJ2P Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
KJ2P Refresh