Back to Data Structures & Algorithms
2025-12-305 min read

Algorithm 2: Find the largest number among three numbers (Data Structures & Algorithms)

Learn Algorithm 2: Find the largest number among three numbers (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

The "Find the Largest Number Among Three Numbers" algorithm is a fundamental building block in data structures and algorithms that tests your grasp of basic programming concepts such as variables, arithmetic operations, comparison operators, and conditional statements. Understanding this problem is crucial for interviews, real-world programming tasks, and debugging common bugs in code.

Prerequisites

To fully understand this lesson, you should be familiar with:

  1. Python syntax and variables
  2. Basic arithmetic operations (addition, subtraction, multiplication, division)
  3. Comparison operators (<, >, ==, !=, <=, >=)
  4. Conditional statements (if, elif, else)
  5. Understanding how to declare functions in Python
  6. Familiarity with data types such as integers and floats

Core Concept

The goal is to write a Python function that takes three numbers as input and returns the largest number among them. Here's an improved version of the solution using conditional statements:

def find_largest(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
else:
return num3

In this code, we first compare num1 with num2. If num1 is greater than both num2 and num3, it returns num1 as the largest number. If not, we move on to comparing num2 with num3 and return the larger one if it's greater than both num1 and num2.

Variations of the Core Concept

  1. Using Python's built-in max() function:
def find_largest(num1, num2, num3):
return max(num1, num2, num3)

This version of the function uses the built-in max() function to find the maximum value among three numbers. It is more concise and efficient than the conditional statement solution but may not help reinforce understanding of comparison operations and conditional statements.

  1. Handling negative numbers:

When dealing with negative numbers, it's important to consider their absolute values when comparing. Here's a modified version of the function that handles both positive and negative numbers:

def find_largest(num1, num2, num3):
if abs(num1) > abs(num2) and abs(num1) > abs(num3):
return num1
elif abs(num2) > abs(num1) and abs(num2) > abs(num3):
return num2
else:
return num3

In this code, we first take the absolute value of each number before comparing them. This ensures that negative numbers do not affect the comparison results.

Worked Example

Let's test our functions with some examples:

Using Conditional Statements

print(find_largest(5, 7, 3)) # Output: 7
print(find_largest(-2, -8, 10)) # Output: 10
print(find_largest(4, 4, 4)) # Output: 4

In the first example, 5 is greater than both 3 and 7, so it's returned as the largest number. In the second example, 10 is greater than both -8 and -2, so it's returned as the largest number. In the third example, all three numbers are equal in absolute value, but since our function returns the first one (4) in such a scenario, it outputs 4.

Using Python's built-in max() function

print(max(5, 7, 3)) # Output: 7
print(max(-2, -8, 10)) # Output: 10
print(max(4, 4, 4)) # Output: 4

In these examples, the built-in max() function returns the largest number among the three input numbers.

Common Mistakes

  1. Comparing with == instead of >:
def find_largest(num1, num2, num3):
if num1 == num2 and num1 > num3:
return num1
elif num2 == num3 and num2 > num1:
return num2
else:
return num3

This version of the function will not work correctly because it checks for equality before checking for greaterness, which leads to incorrect results when more than two numbers are equal.

  1. Forgetting parentheses:
def find_largest(num1, num2, num3):
if num1 > num2 and num2 > num3:
return num1

This version of the function will not work correctly because it checks whether num2 is greater than both num1 and num3, but the correct condition should be that num1 is greater than num2 and num2 is greater than num3.

Common Mistakes (Additional)

  1. Neglecting to handle edge cases:
def find_largest(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1: # Forgetting the comparison with num3
return num2
else:
return num3

This version of the function will not work correctly because it fails to compare num2 with num3. This can lead to incorrect results when two numbers are equal and the third number is larger.

Practice Questions

  1. Write a function to find the second-largest number among three numbers in Python.
  2. Modify the find_largest function to handle negative numbers correctly, considering their absolute values when comparing.
  3. Write a Python program that finds the largest and smallest numbers among an arbitrary list of numbers.
  4. Write a recursive version of the find_largest function.
  5. Extend the find_largest function to find the third-largest number among four or more numbers.

FAQ

A: In this case, any of the numbers can be considered the largest number. Our function returns the first one (num1) in such a scenario. However, you may want to modify the function to return an error message or handle this edge case differently based on your specific requirements.

Q: Can I use Python built-in functions to solve this problem?

A: Yes, you can use Python's built-in max() function to find the maximum value among three numbers:

print(max(5, 7, 3)) # Output: 7

However, writing your own solution helps reinforce your understanding of comparison operations and conditional statements.

Q: How can I find the second-largest number among three numbers in Python?

A: To find the second-largest number among three numbers, you first need to find the largest number using the find_largest function or the built-in max() function and then subtract it from each of the original numbers. The remaining number with the highest value will be the second-largest number:

def find_second_largest(num1, num2, num3):
largest = max(num1, num2, num3)
if (num1 != largest and abs(num1 - largest) > abs(num2 - largest)) or \
(num2 != largest and abs(num2 - largest) > abs(num1 - largest)):
return largest - num1 if num1 != largest else largest - num2
else:
raise ValueError("All numbers are equal")

In this code, we first find the largest number among num1, num2, and num3. Then, we check if either num1 or num2 is not the largest number and has a larger difference with the largest number than the other one. If so, we return the second-largest number as the difference between the largest number and the appropriate smaller number. Otherwise, we raise an error because all numbers are equal.

Algorithm 2: Find the largest number among three numbers (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn