- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
This program determines the largest of two numbers using a simple if-else statement. The user provides two numbers as input, and the program outputs the larger number.
import java.util.Scanner;public class LargestNumber {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// Input two numbersSystem.out.print("Enter the first number: ");int num1 = scanner.nextInt();System.out.print("Enter the second number: ");int num2 = scanner.nextInt();// Determine and print the largest numberif (num1 > num2) {System.out.println("The largest number is: " + num1);} else {System.out.println("The largest number is: " + num2);}}}Copied!✕CopyExample Input/Output:
Input: Enter the first number: 12 Enter the second number: 15
Output: The largest number is: 15
Key Considerations:
This program uses an if-else construct for comparison, which is straightforward and efficient for small inputs.
For more concise code, you can use a ternary operator or Java's built-in Math.max() function.
Java Program to Find largest of Two Numbers - Tutorial Gateway
Program to find the largest of two numbers in Java
Learn how to write a Java program to find the largest of two numbers. Step-by-step guide with code examples for beginners and Java enthusiasts.
Java Program to Find Greatest of Two Numbers
Given two integer input Number1 and Number2, the objective is to write a Java code to compare both the Numbers and Find the Greatest of the Two Numbers. …
java - Find the two largest numbers - Stack Overflow
Dec 19, 2023 · Your code doesn't handle those first two special cases, except by assuming they're bigger than zero. It also doesn't handle shunting the previously largest down to the second largest pile in the …
- Reviews: 8
Java Program To Find the Largest Two Numbers in A given array
Mar 5, 2021 · The below program demonstrates how to find the two largest numbers in an array without using Functions. In this program, firstly we declare and initialize the array.
Write a Java program to find the largest of two numbers. - Filo
Jun 21, 2025 · To find the largest of two numbers in Java, we take input of two numbers from the user, then compare them using an if-else condition. If the first number is greater than the second, we print …
- People also ask
Find Maximum among two numbers in Java - Interview …
Mar 21, 2024 · This Java program demonstrates the usage of the Math.max() method to find the maximum value between two numbers. It defines two …
Java Program to find Largest of Two Numbers - YouTube
In this video, you will learn how to find the largest of two numbers using a simple if-else statement in Java. We are using fixed values for variables a and b — no user input is required.
Java Program to Find Largest of Two Numbers
Java Program to Find Largest of Two Numbers - This article is created to cover some programs in Java that is used to find and print the largest or biggest between two given numbers.
Creating a method to determine the larger of two numbers
In this Assignment I have to write a Java program using command line arguments. There is one method required: getMax, which takes two integer variables as input and returns the bigger one of the two.