Search

## Python Program: Find the Greater of Two Numbers in Python

Learn how to find the greater of two numbers in Python with this comprehensive tutorial. Understand the logic behind comparing numbers using conditional statements and function implementation. Enhance your Python programming skills and gain confidence in handling numerical comparisons. Start exploring this article now and become proficient in finding the greater number in Python.

Let’s see another project – Check if the number is Even or Odd.

### Using Conditional Statements

Example: Using Conditional Statements.

```				```
first_number = int(input("Enter first number: "))
second_number = int(input("Enter second number: "))

if first_number > second_number:
print(f"{first_number} is greater than {second_number}")

elif first_number < second_number:
print(f"{second_number} is greater than {first_number}")

else:
print(f"{first_number} is equal to {second_number}")
```
```

Explanation:

The program prompts the user to enter a number using the input() function. The int() function converts the user input, which is initially a string, into an integer data type.

If statement is used to check the condition (first_number > second_number) whether the first number > second number or not.

If the condition is true, the program executes the code block inside the if statement and prints the message.

If the condition is false, meaning that the first_number is not greater than the second_number. The program moves to the next condition i.e., the elif statement, and checks the condition (first_number < second_number) whether the first number < second number or not.

If the condition is true, the program executes the code block inside the elif statement and prints the message.

If the condition is false, meaning that the first_number is not less than the second_number. The program executes the else statement and prints the message.

### Using a while loop

Example: Using a while loop.

```				```
while True:
first_number = int(input("Enter first number: "))
second_number = int(input("Enter second number: "))

if first_number > second_number:
print(f"{first_number} is greater than {second_number}")

elif first_number < second_number:
print(f"{second_number} is greater than {first_number}")
else:
print(f"{first_number} is equal to {second_number}")

ask_user = input("Do you want to check again? y/n --> ")

print("Thanks for coming. Have a great day ahead!")
break
```
```

Explanation:

Since the code of the block is inside the while loop, the program keeps executing forever until the user stops.

The explanation of the above program is the same as in example 1. However, the program asks the user if they want to continue checking the number; once done, if the user enters “y”, the program executes again, or it terminates the program.

### Using def function

Example: Using the def function.

```				```
def check_num(first_number, second_number):
if first_number > second_number:
return first_number

elif first_number < second_number:
return second_number

else:
return "Both numbers are equal."

number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))

greater_number = check_num(number1, number2)
print("The greater number is:", greater_number)
```
```

Explanation:

The code defines a function called check_num that takes two parameters: first_number and second_number. This function compares the two input numbers and determines which one is greater.

Inside the function, an if statement is used to check the conditions.

If the first_number is greater than the second_number, it returns the first_number. If the second_number is greater, it returns the second_number. If both numbers are equal, it returns the string “Both numbers are equal.

Now, the program prompts the user to enter a number using the input() function stored in the number1 and number2 variables. The int() function converts the user input, which is initially a string, into an integer data type.

The function check_num() is called with number1 and number2 as arguments, and the returned value is assigned to the variable greater_number.

Finally, the greater number is displayed to the user using the print() function.

Overall, this code allows the user to input two numbers and determines which one is greater by calling the check_num() function. It demonstrates the use of conditional statements (if, elif, else) and functions in Python.

### Conclusion

The article covers the implementation of a function that takes two numbers as input and utilizes conditional statements to perform the comparison. It explains the logical flow of the code, including scenarios where one number is greater, the other number is greater, or both numbers are equal.

By following the step-by-step explanations and studying the provided code examples, readers gain a clear understanding of the underlying concepts and the syntax used for number comparison in Python.

Through this article, readers will be equipped with the skills to confidently compare two numbers and determine the greater one in Python. It empowers readers to write efficient and effective code for numerical comparisons, enhancing their overall Python programming skills.

Recent Articles