Find the Greatest of Two Numbers in Python
Find the Greatest of the Two Numbers
Given two integer inputs as number1 and number2, the objective is to find the largest among the two. Therefore we’ll write a code to Find the Greatest of the Two Numbers in Python Language.
Example Input : 20 40 Output : 40
Find the Greatest of the Two Numbers in Python Language
Given two integer inputs, the objective is to find the largest number among the two integer inputs. In order to do so we usually use if-else statements to check which one’s greater. Here are some of the Python methods to solve the above mentioned problem.
- Method 1: Using if-else Statements
- Method 2: Using Ternary Operator
- Method 3: Using inbuilt max() Function
We’ll discuss the above mentioned methods in detail in the upcoming sections.
Method 1: Using if-else Statements
In this method we’ll find the Largest Number using simple if-else statements.
Working
For the two integer inputs num1 and num2
- Check if num1>num2, print num1 if true.
- Print num2 otherwise.
Let’s implement the above logic in Python Language.
Python Code
num1, num2 = 20 , 30 if num1>num2: print(num1) else: print(num2)
Output
30
Method 2: Using Ternary Operator
In this method we’ll use Ternary Operator in Python to find the Largest Number among the two input integers.
Working
Given the integer inputs num1 and num2
- Check if num1>num2 using ternary operator in python.
- print num1 if true or num2 otherwise.
Let’s implement the above logic in Python Language.
Python Code
num1, num2 = 20 , 30 print((num1 if num1>num2 else num2))
Output
30
Method 3: Using inbuilt max() Function
In this method we’ll use the inbuilt max() function that take two integers as arguments and returns the largest among them.
Working
Given two input numbers num1 and num2
- Call inbuilt max() function with num1 and num2 as arguments.
- Print the returned value.
Let’s implement the above logic in Python Language.
Python Code
num1, num2 = 20, 30 print(max(num1,num2))
Output
30
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
N1 = int(input(“Enter the first number : “))
N2 = int(input(“Enter the second number : “))
diff = N1 – N2
if diff 0:
print(f”{N1} is greater”)
elif diff == 0:
print(“Both the numbers are same”)
not included the equality condition
n1 = int(input(“Enter the number 1 : “))
n2 = int(input(“Enter the number 2 : “))
if n1 < n2:
print(f"{n2} is Grater")
# elif n1 == n2:
# print(f"{n1} and {n2} are equal")
else:
print(f"{n1} is grater ")
a=int(input(“Enter the First number: “))
b=int(input(“Enter the Last number: “))
if(a>b):
print(“The greatest number is “,a)
elif(b>a):
print(“The greatest number is “,b)
else:
print(“The numbers are equal”)
In this program there is no case for when numbers are equal here the program for that case too.