π§ 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)
- We ask the user to enter 3 numbers.
- We use
if-elseto compare:
- Is
abigger than bothbandc? If yes, itβs the largest. - If not, is
bthe biggest? - If none of those, then
cmust be the largest.
- We store the biggest number in the variable
largest. - 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. π

