Java Program to Find the Largest of Three Numbers

Introduction

Finding the largest of three numbers is a common programming task that helps you understand conditional statements and comparisons. This guide will walk you through writing a Java program that determines the largest of three given numbers.

Problem Statement

Create a Java program that:

  • Prompts the user to enter three integers.
  • Compares the three integers to determine the largest.
  • Displays the largest number.

Example:

  • Input: a = 10, b = 25, c = 20

  • Output: "The largest number is 25"

  • Input: a = 5, b = 5, c = 3

  • Output: "The largest number is 5"

Solution Steps

  1. Read the Three Numbers: Use the Scanner class to take three integers as input from the user.
  2. Compare the Numbers: Use conditional statements (if-else) to compare the three numbers and find the largest.
  3. Display the Result: Print the largest number.

Java Program

// Java Program to Find the Largest of Three Numbers // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class LargestOfThreeNumbers { public static void main(String[] args) { // Step 1: Read the three numbers from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter the first number (a): "); int a = scanner.nextInt(); System.out.print("Enter the second number (b): "); int b = scanner.nextInt(); System.out.print("Enter the third number (c): "); int c = scanner.nextInt(); // Step 2: Compare the numbers to find the largest int largest; if (a >= b && a >= c) { largest = a; } else if (b >= a && b >= c) { largest = b; } else { largest = c; } // Step 3: Display the result System.out.println("The largest number is " + largest); } } } 

Explanation

Step 1: Read the Three Numbers

  • The Scanner class is used to read three integer inputs from the user. The nextInt() method captures each number.

Step 2: Compare the Numbers

  • The program uses a series of if-else statements to compare the three numbers:
    • If a is greater than or equal to both b and c, then a is the largest.
    • If b is greater than or equal to both a and c, then b is the largest.
    • Otherwise, c is the largest.

Step 3: Display the Result

  • The program prints the largest number using System.out.println().

Output Example

Example 1:

Enter the first number (a): 10 Enter the second number (b): 25 Enter the third number (c): 20 The largest number is 25 

Example 2:

Enter the first number (a): 5 Enter the second number (b): 5 Enter the third number (c): 3 The largest number is 5 

Example 3:

Enter the first number (a): 15 Enter the second number (b): 8 Enter the third number (c): 20 The largest number is 20 

Conclusion

This Java program demonstrates how to find the largest of three numbers using conditional statements. It covers essential concepts such as comparison operations and conditional logic, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Scroll to Top