Write a Java program to Find the smallest number between two numbers using the library method


This Java program reads two double values from the user and then uses the Math.min method to determine the smallest of the two values. Here's a breakdown of the program:

  • The import java.util.*; statement imports the java.util package, which contains the Scanner class that is used to read input from the user.
  • The public class Smallest_Number statement declares a public class called Smallest_Number, which is the name of the Java file that this program is saved in.
  • The public static void main(String[] args) method is the entry point for the program. It takes an array of strings as an argument, but in this program, that argument is not used.
  • Inside the main method, a new Scanner object called input is created to read input from the user.
  • Two double variables num1 and num2 are declared and initialized to 0.
  • The program prompts the user to enter two double values using System.out.print and reads them using input.nextDouble().
  • The Math.min(num1, num2) method is used to determine the smallest of the two values entered by the user.
  • The result of the Math.min method is printed to the console using System.out.print.

Source Code

import java.util.*;
public class Smallest_Number
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		double num1 = 0;
		double num2 = 0;
 
		System.out.print("Enter The Number : ");
		num1 = input.nextDouble();
 
		System.out.print("Enter The Number : ");
		num2 = input.nextDouble();
 
		System.out.print("Smallest Number is : " + Math.min(num1, num2));
	}
}

Output

Enter The Number : 53.215
Enter The Number : 42.6
Smallest Number is : 42.6

Example Programs