Greatest of Two Numbers in Python: Simple Program

Greatest of Two Numbers in Python: Simple Program

Find the Greatest of Two Numbers in PythonIn this article, we will explore different methods to determine the greatest of two numbers using Python. This is a common problem-solving exercise for beginners to understand conditional statements, built-in functions, and basic arithmetic operations.

Input and Output Format

Input Format:
  • Two integers provided by the user.
Output Format:
  • A single integer that is the greater of the two input numbers.
Sample Input:
45
86
Sample Output:
86

Prerequisite Knowledge

Before diving into the implementation, ensure familiarity with:
  • Python Built-In Functions
  • Conditional Statements
  • Arithmetic Operators

Method 1: Using the Built-In Function max()

The max() function in Python simplifies finding the greater number between two inputs.

Algorithm

  1. Accept two integer inputs from the user.
  2. Use the max() function to find the larger of the two numbers.
  3. Print the result.

Python Code

# Python program to find the greatest of two numbers using the built-in function
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print(max(num1, num2), "is greater")
Input:
Enter the first number: 23
Enter the second number: 45
Output:
45 is greater

Method 2: Using if-else Statements

if-else StatementsThis approach relies on conditional statements to compare two numbers.

Algorithm

  1. Get two integer inputs from the user.
  2. Compare the two numbers using an if-else block.
  3. Print the greater number.

Python Code

# Python Program to find the greatest of two numbers using if-else statements
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a >= b:
    print(a, "is greater")
else:
    print(b, "is greater")
Input:
Enter the first number: 50
Enter the second number: 45
Output:
50 is greater

Method 3: Using Arithmetic Operators

Arithmetic OperatorsYou can determine the greater number by leveraging arithmetic operations like subtraction.

Algorithm

  1. Accept two integer inputs from the user.
  2. Subtract the second number from the first.
  3. Use the result to determine which number is greater.
  4. Print the larger number.

Python Code

# Python Program to find the largest of two numbers using an arithmetic operator
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a - b > 0:
    print(a, "is greater")
else:
    print(b, "is greater")
Input:
Enter the first number: 90
Enter the second number: 100
Output:
100 is greater

Conclusion

  • Python provides multiple methods to determine the greater of two numbers.
  • The max() function is a quick and efficient way, while if-else statements and arithmetic operations offer more control and clarity for beginners.
  • Understanding these methods builds a strong foundation for solving more complex problems.
Start experimenting with these approaches and expand your knowledge of Python programming Click here to Know more our program Greatest of Two Numbers in Python 
c