Google News
logo
Java Program to Find the Largest Among Three Numbers
In the following example Java program to find the largest among three numbers :
Program :
import java.util.Scanner;

public class LargestNumberFinder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three numbers: ");
        double num1 = input.nextDouble();
        double num2 = input.nextDouble();
        double num3 = input.nextDouble();

        double largest = num1;

        if (num2 > largest) {
            largest = num2;
        }

        if (num3 > largest) {
            largest = num3;
        }

        System.out.println("The largest number is " + largest);
    }
}
Output :
Enter three numbers: 12
7
18
The largest number is 18.0
In this program, we first ask the user to input three numbers using the Scanner class. Then, we initialize a variable largest to the value of the first number num1.

We then use two if statements to compare num2 and num3 with largest. If num2 is greater than largest, we update the value of largest to num2. Similarly, if num3 is greater than largest, we update the value of largest to num3.

Finally, we print the value of largest to the console using System.out.println().