Skip to main content

← Back to Overview

Greatest of Three Numbers

Concept Explanation

What is it?

This program determines the largest value among three given numbers. It's a fundamental exercise in applying conditional logic (specifically nested if-else statements) to compare multiple values and make a decision based on those comparisons.

Why is it important?

Finding the maximum or minimum of a set of values is a very common task in programming. It forms the basis of many algorithms, from simple data processing to more complex sorting or selection processes. Understanding how to compare multiple values effectively is a core programming skill.

Where is it used?

  • Data Analysis: Finding the highest score, maximum temperature, or peak sales.
  • Game Development: Determining the highest stat of a character, the best score, or comparing strengths.
  • Resource Allocation: Identifying the most available resource among several options.
  • Ranking Systems: Basic logic for ranking items or entities.

Real-world example

Imagine you have three friends, each with a different amount of money. To find out who has the most money, you compare their amounts. If Alice has more than Bob, you then compare Alice's amount with Charlie's to see if Alice still has the most. This sequential comparison mimics the logic of nested if-else statements.


Algorithm

  1. Start.
  2. Get three numbers (num1, num2, num3) from the user.
  3. Assume num1 is the greatest initially.
  4. Compare num1 with num2: a. If num1 is greater than or equal to num2: i. Compare num1 with num3. ii. If num1 is greater than or equal to num3, then num1 is the greatest. iii. Else, num3 is the greatest. b. Else (num2 is greater than num1): i. Compare num2 with num3. ii. If num2 is greater than or equal to num3, then num2 is the greatest. iii. Else, num3 is the greatest.
  5. Display the greatest number.
  6. End.

Edge Cases:

  • Inputting non-numeric values (handled by language-specific error mechanisms or explicit validation).
  • All numbers are equal: The program should correctly identify any of them as the "greatest."
  • Two numbers are equal and greater than the third: e.g., (10, 10, 5) - should correctly identify 10.

Implementations

import java.util.Scanner;

public class GreatestOfThreeNestedIf {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();

System.out.println("Numbers entered: " + num1 + ", " + num2 + ", " + num3);

if (num1 >= num2) {
if (num1 >= num3) {
System.out.println(num1 + " is the greatest number.");
} else {
System.out.println(num3 + " is the greatest number.");
}
} else { // num2 > num1
if (num2 >= num3) {
System.out.println(num2 + " is the greatest number.");
} else {
System.out.println(num3 + " is the greatest number.");
}
}

scanner.close();
}
}

Explanation

  • Java: Uses nested if-else statements to compare numbers. Scanner handles double input.
  • Python: Similar nested if-else logic. float() converts input strings to numbers.
  • C: Employs nested if-else statements. scanf("%lf", ...) reads double inputs, and printf("%.2lf, ...) formats output.
  • Oracle: Implemented in PL/SQL. Uses nested IF-ELSE blocks. Substitution variables for interactive input.

Complexity Analysis

  • Time Complexity: O(1) - A fixed number of comparisons are performed.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

Sample Dry Run

Stepnum1num2num3Description
Input1057User enters 10, 5, 7
num1 >= num2?1057True (10 >= 5)
num1 >= num3?1057True (10 >= 7)
Output10--Display "10 is the greatest number."
End---Program terminates
Input5128User enters 5, 12, 8
num1 >= num2?5128False (5 >= 12 is False)
num2 >= num3?5128True (12 >= 8)
Output-12-Display "12 is the greatest number."
End---Program terminates

Practice Problems

Easy

  • Modify the program to find the smallest of three numbers.
  • Find the greatest of two numbers.

Medium

  • Implement finding the greatest of N numbers (e.g., using an array/list and a loop).
  • Find the second greatest number among three.

Hard

  • Find the three greatest distinct numbers from a list of numbers.

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir