Program to Find the Greatest of Two Numbers in Python

Finding the Greatest of Two Numbers

Given two integer inputs num1 and num2, the objective is to determine which number is greater using Python programming.

We will explore different methods to achieve this.

Method 1: Using if-else Statement

We use an if-else statement to compare two numbers and determine the greatest one.

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

if num1 > num2:
    print(f"{num1} is the greatest number")
elif num2 > num1:
    print(f"{num2} is the greatest number")
else:
    print("Both numbers are equal")
            

Output:

Enter first number: 5
Enter second number: 10
10 is the greatest number

Method 2: Using Ternary Operator

We use the ternary operator to find the greatest number in a single line.

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

greatest = num1 if num1 > num2 else num2
print(f"{greatest} is the greatest number")
            

Output:

Enter first number: 7
Enter second number: 3
7 is the greatest number

Method 3: Using Function

We create a function to compare two numbers and return the greatest one.

def find_greatest(a, b):
    return a if a > b else b

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

print(f"{find_greatest(num1, num2)} is the greatest number")
            

Output:

Enter first number: 4
Enter second number: 9
9 is the greatest number
Easy aceess next quctions
Getting Started

Positive or Negative number: C C++ Java Python

Even or Odd number: C C++ Java Python

Sum of First N Natural numbers: C C++ Java Python

Sum of N natural numbers: C C++ Java Python

Sum of numbers in a given range: C C++ Java Python

Greatest of two numbers: C C++ Java Python

Greatest of the Three numbers: C C++ Java Python

Leap year or not: C C++ Java Python

Prime number: C C++ Java Python

Prime number within a given range: C C++ Java Python

Sum of digits of a number: C C++ Java Python

Reverse of a number: C C++ Java Python

Palindrome number: C C++ Java Python

Armstrong number: C C++ Java Python

Armstrong number in a given range: C C++ Java Python

Harshad number: C C++ Java Python

Abundant number: C C++ Java Python

Friendly pair: C C++ Java Python