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


This Java program reads two double values from the user and then uses the Math.max method to determine the largest 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 Largest_Number statement declares a public class called Largest_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.max(num1, num2) method is used to determine the largest of the two values entered by the user.
  • The result of the Math.max method is printed to the console using System.out.print.

Source Code

import java.util.*;
public class Largest_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 1 : ");
		num1 = input.nextDouble();
 
		System.out.print("Enter The Number 2 : ");
		num2 = input.nextDouble();
 
		System.out.print("Largest Number is : " + Math.max(num1, num2));
	}
}

Output

Enter The Number 1 : 85
Enter The Number 2 : 101
Largest Number is : 101.0

Example Programs