Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java program to find maximum of two numbers using Math.max()

  • We can find maximum of two numbers by using if condition check.
  • In java.lang.Math class also having a method Math.max() which will take two numbers and returns maximum number.
  • We have separate methods for int , float , double and long.




  1.  public static double max(double a, double b)

Java program to find maximum of two double values using Math.max() method.


  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9.   public static void main(String[] args) {
  10.         
  11.         
  12.         double a = 234567;
  13.         double b =234557;
  14.      
  15.         double x = 764554;
  16.         double y =764464;
  17.         
  18.         System.out.println("Math.max(" + a + "," + b + ")=" + Math.max(a, b));
  19.         System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));
  20.         
  21.     }
  22.  
  23. }
Output:

  1. Math.pow(3.0,4.0)=81.0
  2. Math.pow(4.0,3.0)=64.0
  3. Math.pow(4,2)=16.0

Java program to find maximum of two float values using Math.max() method.





Java program to find maximum of two int values using Math.max() method.
 
  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9. public static void main(String[] args) {
  10.         
  11.         System.out.println( Math.max(1, 1));
  12.         System.out.println( Math.max(1, 2));
  13.         System.out.println(Math.max(1.222f, 1.223f));
  14.         System.out.println(Math.max(1.222, 1.223));
  15.         
  16.     }
  17.  
  18. }

Output:

  1. 1
  2. 2
  3. 1.223
  4. 1.223


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

Java program to find maximum of two numbers using Math.max()

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×