Largest of Three Numbers
Find the largest of three numbers using conditional statements.
BeginnerTopic: Module 1: Basic Java Programs
Java Largest of Three Numbers Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter second number: ");
double b = sc.nextDouble();
System.out.print("Enter third number: ");
double c = sc.nextDouble();
double largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
System.out.println("Largest = " + largest);
sc.close();
}
}Output
Enter first number: 5 Enter second number: 12 Enter third number: 9 Largest = 12.0
Understanding Largest of Three Numbers
We use a series of comparisons (>=) to determine which of the three numbers is the largest.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.