- 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.
- public static double max(double a, double b)
Java program to find maximum of two double values using Math.max() method.
- package com.mathmax;
- public class MathMAx {
- /**
- * Math.max() method in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- double a = 234567;
- double b =234557;
- double x = 764554;
- double y =764464;
- System.out.println("Math.max(" + a + "," + b + ")=" + Math.max(a, b));
- System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));
- }
- }
- Math.pow(3.0,4.0)=81.0
- Math.pow(4.0,3.0)=64.0
- 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.
- package com.mathmax;
- public class MathMAx {
- /**
- * Math.max() method in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- System.out.println( Math.max(1, 1));
- System.out.println( Math.max(1, 2));
- System.out.println(Math.max(1.222f, 1.223f));
- System.out.println(Math.max(1.222, 1.223));
- }
- }
Output:
- 1
- 2
- 1.223
- 1.223
No comments