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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad