Java program to Implement Mathematical functions defined in Java Math class

  • Max(parameters) Method- Return the maximum value
    • Static int max(int x, int y)
    • Static long max(int x, int y)
    • Static double max(int x, int y)
    • Static float max(int x, int y)
  • Min(parameters) method- Return the minimum value
    • Static int min(int x, int y)
    • Static int min(int x, int y)
    • Static int min(int x, int y)
    • Static int min(int x, int y)
Java Program:
import java.lang.*;
import java.util.*;

public class minmax
{
  public static void main(String args[])
  {
  int x=10,y=20;
  int x=Math.min(x,y);
  System.out.println("Minimum value between 10 & 20 is "+z);
  int x=Math.max(x,y);
  System.out.println("Maxmimum value between 10 & 20 is "+z);
  }
}
output:
 Minimum value between 10 & 20 is 10
 Maximum value between 10 & 20 is 20

Absolute Method Defined by Java Math class

  • abs(parameter):- Return the absolute value of argument
    • static int abs(int arg)
    • static long abs(long arg)
    • static float abs(float arg)
    • static double abs(double arg)
Java program to use absolute method:
import java.util.*;
import java.lang.*;
public class absolute
{
 public static void main(String args[])
 {
  double a=1.500;
  double z=Math.abs(a);
  System.out.println("Absolute of 1.500 is")
 }
}
output:
Absolute of 1.500 is 1.5

Round off method defined in Java Math class

  • round(parameter):- return arguments rounded value.
    • static int round(float args)
    • static int round( double args)
import java.util.*;
import java.lang.*;
class Round
{
 public static void main(String args[])
 double a=1.50,b=1.49;
 double z=Math.round(a);
 System.out.println("Round value of 1.50= "+z);
 double x=Math.round(a);
 System.out.println("Round value of 1.49= "+x);
}
}

Output:
Round value of 1.50= 2.0
Round value of 1.49= 1.0

Other Methods Defined in Java Math class

  • sqrt()- Used to return the Square root of the number.
    • Syntax :- static double sqrt(double arg)
  • pow()- Used to return power of given number.
    • Syntax :- static double pow(double y, double x)
Java program:
import java.lang.*;
import java.util.*;
public class example
{
 public static void main(String args[])
{
 int a=16,b=2;
 double z=Math.sqrt(a);// to find square root
 System.out.println("Square of Number is:-" +z);
 double x=Math.pow(a,b);
 System.out.println("Power of given Number:-");
}
}
Output:
Square root of Number is:- 4
Power of given Number:-256
  • Describe various method defined in java Math class?
  • Write a program to find min and max number?
  • Write a program to find square root and power of given number?
  • Explain with programs various Math methods in java

Leave a Comment