Python program to find the greatest among two numbers
Python program to find the greatest among two numbers
We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them. This python program needs the user to enter two different values. Next, Python will compare these numbers and print the greatest among them, or both are equal.
Example:
Input: A = 5, B = 6 Output: B is greater than A
Source Code
The following is the source code that print the greatest amongst them or both numbers are equal:
#Take two number from user to compare num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) #Compare both the number if num1 >= num2: if num1 == num2: print("Both numbers are equal.") else: print("Fisrt number is greater than the second number.") else: print("Second number is greater than the First number.")
Output
Case 1:

Case 2:

Case 3:

Explanation: In the above code, we have asked a user to take two integer values as an input and store them in a separate variable. After taking the inputs, it will compare both the numbers and check that which number is greater or both the number is equal by using if....else statement. After performing the comparison, the final result will be printed on the screen.
Largest of Two Numbers using Arithmetic Operator
In this Python programs, we are using a Minus operator to find the greatest of Two Numbers:
#Program to find the greatest among two number using operators #Take two number from user to compare num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) #Compare both the number if(num1 - num2 > 0): print("{0} is largest than {1}".format(num1, num2)) elif(num2 - num1 > 0): print("{0} is largest than {1}".format(num2, num1)) else: print("Both numbers are Equal")
Output
Here is the result:
