Let's take a look at the Math class from java.lang which has the static max and min methods that can be used to find the max and the min of two numbers passed in as a input argument.
Example:package org.example;
public class MaxMinAvgExample {
public static void main(String[] args) {
int no1 = 10;
int no2 = 20;
//max
System.out.println("Max number is: " + Math.max(no1, no2));
//min
System.out.println("Min number is: " + Math.min(no1, no2));
}
}
Output:
Max number is: 20
Min number is: 10
More Examples:
Math.max(14, 12) -> Output: 12
Math.min(-1, 2) -> Output: -1
Math.max(14.2, 14.3) -> Output: 14.3
Math.min(2.2, 4.5) -> Output: 2.2
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!