Placement Prep

Greatest of Three Numbers in Python: 3 Methods

Three Python methods to find the greatest of three numbers: if-else comparison, built-in max(), and a custom function. Sample runs and edge-case handling included.

By FACE Prep Team 5 min read
python if-else max-function comparison placement-prep python-programs coding-practice

Finding the greatest of three numbers in Python is one of the first comparison problems in any placement coding syllabus: it tests whether you can chain conditions correctly without missing an edge case.

Three methods appear consistently in TCS, Infosys, and Wipro campus coding rounds: an if-else chain, the built-in max() function, and a custom function that wraps the logic for reuse. This article covers all three with working code, sample runs, and edge-case analysis.

Input and output format

The program reads three integers (entered one per line by the user) and prints the greatest of the three. A typical run looks like this:

  • Input: 44, 12, 76 (entered on separate prompts)
  • Output: The greatest number is 76

The answer here is 76 because 76 is greater than both 44 and 12. All three methods produce the same output; they differ only in how the comparison is structured internally.

Before writing any comparison logic, it helps to see the problem in the companion article on finding the greatest of two numbers, which covers the same if-else pattern for a two-variable case. The three-number version adds one extra comparison branch to that foundation.

Method 1: if-else statement

The if-else method compares each number against the other two. The first branch checks whether num1 is at least as large as both num2 and num3. The elif branch checks whether num2 is at least as large as num3 (num1 was already ruled out). The else branch handles the remaining case, which means num3 must be the greatest.

# Python program to find the greatest of three numbers using if-else

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))

if (num1 >= num2) and (num1 >= num3):
    greatest = num1
elif (num2 >= num3):
    greatest = num2
else:
    greatest = num3

print("The greatest number is", greatest)

Sample run:

  • Input: 44, 12, 76
  • Evaluation: 44 >= 12 is true, but 44 >= 76 is false, so the if branch fails. 12 >= 76 is false, so the elif branch fails. Else fires, setting greatest = 76.
  • Output: The greatest number is 76

This is also the same if-elif pattern used in the simple calculator program in Python, where different operations are selected through chained conditions. The structural logic is identical.

Method 2: Built-in max() function

Python’s built-in max() function accepts two or more arguments and returns the largest. Passing all three numbers in a single call replaces the entire if-else block.

# Python program to find the greatest of three numbers using max()

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))

print(max(num1, num2, num3), "is the greatest")

Sample run:

  • Input: 100, 200, 150
  • Output: 200 is the greatest

The max() call is O(n) internally: it iterates through all arguments once and returns the largest. For three values, that is three comparisons at most, identical to what the if-else chain does manually.

Most placement platforms (AMCAT, TCS NQT, Infosys InfyTQ) allow Python built-ins in the coding section unless the problem statement specifies otherwise. When the question is “write a Python program to find the greatest,” max() is a valid and clean answer. When the question says “without using built-in functions,” use Method 1 or Method 3.

Method 3: Custom function

A custom function isolates the comparison logic into a named block that can be called multiple times from different parts of a program. This is the right choice when the greatest-of-three check is not a one-time operation but a helper needed throughout a larger script.

# Python program to find the greatest of three numbers using a custom function

def find_greatest(a, b, c):
    if (a >= b) and (a >= c):
        largest = a
    elif (b >= c):
        largest = b
    else:
        largest = c
    return largest

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))

print("The greatest number is", find_greatest(num1, num2, num3))

Sample run:

  • Input: 256, 24, 123
  • Evaluation: 256 >= 24 is true and 256 >= 123 is true, so the if branch fires and largest = 256.
  • Output: The greatest number is 256

The Python tutorial on defining functions covers the mechanics of def, parameters, and return in detail; worth a quick read if the function structure above looks unfamiliar.

The Python basic programs collection includes similar examples (factorial, Fibonacci, prime check) that follow the same function-definition pattern. Practicing them together builds the pattern-recognition that placement coding rounds test.

Handling equal numbers

Two scenarios need checking: when two numbers are equal and tied for greatest, and when all three are equal.

Two numbers tied for greatest

Consider inputs 7, 7, 3:

  • if branch: 7 >= 7 is true AND 7 >= 3 is true — the condition holds.
  • greatest = num1 = 7. Correct.

Consider inputs 3, 7, 7:

  • if branch: 3 >= 7 is false — fails immediately.
  • elif branch: 7 >= 7 is true — greatest = num2 = 7. Correct.

All three equal

Consider inputs 5, 5, 5:

  • if branch: 5 >= 5 is true AND 5 >= 5 is true — fires.
  • greatest = 5. Correct.

The >= operator is the key: it ensures that ties resolve cleanly instead of falling through all conditions and raising an error. A common mistake is using strict > throughout, which causes the if-else chain to fail when the top two numbers are equal.

Which method fits which situation

ScenarioRecommended method
Learning conditionals for the first timeMethod 1 (if-else)
Placement test with no built-in restrictionMethod 2 (max())
Placement test that bans built-insMethod 1 or Method 3
Greatest-of-three needed in multiple placesMethod 3 (custom function)
Extending to four or more numbersMethod 2 (max() scales directly)

Method 1 makes the comparison logic visible: every branch is explicit, which is useful when an interviewer asks you to walk through the execution. Method 2 is the production-Python choice: one line, readable, and consistent with how Python code is written in real projects. Method 3 sits between them: it uses the same if-else logic as Method 1 but packages it for reuse, which is the right instinct the moment a helper function is called more than once.

The custom function in Method 3 does one thing: it takes three inputs, applies a comparison rule, and returns one output. That same input-process-output structure scales to more complex programming tasks. If you want to move from functions that compare three integers to building something that runs on an LLM API, TinkerLLM is the entry point at ₹499, where FACE Prep readers apply that same function-call discipline to hands-on AI projects they can add to a portfolio.

Primary sources

Frequently asked questions

What is the simplest way to find the greatest of three numbers in Python?

Use Python's built-in max() function: max(num1, num2, num3) returns the largest of the three in a single call. This is the most concise and readable approach when built-ins are not restricted by the platform.

What does Python's max() return when two numbers are equal?

max() returns the greatest value regardless of duplicates. If two numbers share the maximum value, it returns that value once. For example, max(7, 7, 3) returns 7.

How do I find the greatest of three numbers without using max()?

Use an if-else chain: check if num1 >= num2 and num1 >= num3; if not, check if num2 >= num3; otherwise num3 is the greatest. This is the standard approach when built-ins are off-limits in a placement test.

Which method is expected in a placement coding test?

Read the problem statement. If it says 'without using built-in functions', write the if-else or custom-function version. If no such restriction exists, max() is perfectly acceptable and preferred by most interviewers for readability.

What happens if all three numbers are the same?

The if-else check uses >= (greater than or equal to), so the condition num1 >= num2 and num1 >= num3 is true when all three are equal. The program returns num1, which equals all three, without any error.

Can I use these methods to compare more than three numbers?

The max() method scales directly: max(a, b, c, d) works for four numbers. The if-else and custom-function approaches would need additional branches for each extra number, which is why max() is preferred when comparing many values.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹499)
Free AI Roadmap PDF