Home »
Java »
Java Programs
Java program to find the largest number between two numbers using the library method
Given two numbers, we have to find the largest number between two numbers using the library method.
Submitted by Nidhi, on May 16, 2022
Problem statement
In this program, we will read a floating-point number from the user using the Scanner class and find the largest number between two numbers using the Math.max() method.
Source Code
The source code to find the largest number between two numbers using the library method is given below. The given program is compiled and executed successfully.
// Java program to find the largest number between two numbers
// using the library method
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner X = new Scanner(System.in);
double num1 = 0;
double num2 = 0;
System.out.print("Enter number1: ");
num1 = X.nextDouble();
System.out.print("Enter number2: ");
num2 = X.nextDouble();
System.out.print("Largest number is: " + Math.max(num1, num2));
}
}
Output
Enter number1: 56.34
Enter number2: 34.56
Largest number is: 56.34
Explanation
In the above program, we imported the "java.util.*" package to use the Scanner class. Here, we created a public class Main that contains a main() method.
The main() method is the entry point for the program. Here, we created two variables num1, and num2 of double type and read their value from the user using the nextDouble() method of the Scanner class. Then we found the largest number between two numbers using the Math.max() method and printed the result.
Java Math Class Programs »