Daily Java Challenge #3 – Find the Largest of Three Numbers (Beginner Level)

Find the Largest of Three Numbers
  • Save
Find the Largest of Three Numbers

🧠 Problem Statement

Write a Java program to find the largest among three numbers entered by the user.


πŸ“₯ Input

Three numbers, for example:
25, 89, 41


πŸ“€ Output

A message like:
The largest number is 89.


πŸ’‘ Simple Explanation

Let’s imagine three boxes with numbers inside. We want to peek inside all three boxes and figure out which number is the biggest.

We’ll use if-else conditions to compare the numbers one by one.


🧾 Java Code

import java.util.Scanner;

public class LargestOfThree {
    public static void main(String[] args) {
        // Step 1: Ask the user for three numbers
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = scanner.nextInt();

        System.out.print("Enter second number: ");
        int b = scanner.nextInt();

        System.out.print("Enter third number: ");
        int c = scanner.nextInt();

        // Step 2: Compare them 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: Print the result
        System.out.println("The largest number is " + largest + ".");
    }
}

πŸ” Step-by-Step Explanation (Like You’re Brand New to Java)

  1. We ask the user to enter 3 numbers.
  2. We use if-else to compare:
  • Is a bigger than both b and c? If yes, it’s the largest.
  • If not, is b the biggest?
  • If none of those, then c must be the largest.
  1. We store the biggest number in the variable largest.
  2. We display the result.

πŸ“š Example

πŸ‘‰ Input: 45, 78, 12
βœ… Output: The largest number is 78.

πŸ‘‰ Input: 5, 5, 5
βœ… Output: The largest number is 5.


🎯 Mini-Challenge for You

Try writing this program using nested if or even a ternary operator (if you’re feeling adventurous!).


πŸ“ See you tomorrow with Challenge #4!
Let’s keep learning Java together, one baby step at a time. πŸš€

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *