if-else
statements to determine which number is the largest:
public class LargestOfThreeNumbers
{
public static void main(String[] args)
{
int no1=10, no2=20, no3=30;
// Finding and displaying the largest number
if (no1>no2 && no1>no3)
{
System.out.println("\nThe largest number is : " + no1);
}
else if (no2>no1 && no2>no3)
{
System.out.println("\nThe largest number is : " + no2);
}
else
{
System.out.println("\nThe largest number is : " + no3);
}
}
}
The largest number is : 30
import java.util.Scanner;
public class LargestOfThreeNumbers
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number 1: ");
int no1 = scanner.nextInt();
System.out.print("Enter number 2: ");
int no2 = scanner.nextInt();
System.out.print("Enter number 3: ");
int no3 = scanner.nextInt();
// Finding and displaying the largest number
if (no1>no2 && no1>no3)
{
System.out.println("\nThe largest number is : " + no1);
}
else if (no2>no1 && no2>no3)
{
System.out.println("\nThe largest number is : " + no2);
}
else
{
System.out.println("\nThe largest number is : " + no3);
}
scanner.close();
}
}
Enter number 1: 100 Enter number 2: 200 Enter number 3: 300 The largest number is : 300
Scanner
class is imported to take user input.
no1
, no2
and no3
) are declared to store the user’s input.
scanner.nextInt()
to read integer values from the user.
if
condition checks if no1
is greater than or equal to both no2
and no3
. If true, num1 is the largest.
else if
condition checks if no2
is greater than or equal to both no1
and no3
. If true, no2
is the largest.
no3
is the largest.
scanner.close()
method is used to close the input stream and release resources.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.