Programming Examples
Java program to find min and max
Write a Java program that takes two numbers as input and calculates the maximum and minimum of the two using Math.max() and Math.min() functions.
Solution
import java.util.Scanner;
public class MaxMinCalculation
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
double number1 = sc.nextDouble();r
System.out.print("Enter the second number: ");
double number2 = sc.nextDouble();
double max = Math.max(number1, number2);
double min = Math.min(number1, number2);
System.out.println("The maximum of " + number1 + " and " + number2 + " is " + max);
System.out.println("The minimum of " + number1 + " and " + number2 + " is " + min);
}
}
Output