PYTHON PROGRAM TO FIND MAXIMUM OF TWO NUMBERS

In this topic, we will discuss how to easily and interestingly to find maximum of two numbers using Python programming.  Let’s explore the methods we will use to find maximum of two numbers.

To Find Maximum Of Two Numbers

 

Table of Contents:

  • Introduction
  • Program to find max of two numbers
  • Output
  • Conclusion

Introduction:

To find the maximum of two numbers efficiently, utilize appropriate methods and functions.

The methods are,

  1. if-else
  2. Using max() function.
  3. Using sort() method.

if-else condition:

This approach uses an if-else statement to compare two numbers and prints the result based on the comparison.

Example Program:

a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
if (a>b):
   print("The greatest value is ",a)
else:
   print("The greatest value is ",b)
   

Output:

Enter the value of a: 4

Enter the value of b:6

The greatest value is 6

Using max() function:

The function ‘max()’ finds the maximum of the values passed as its arguments.

Example Program:

a=int(input("Enter the value of a:")) 
b=int(input("Enter the value of b:"))
maximum = max(a, b)
print("The Maximum value of two numbers:" ,maximum)

Output:

Enter the value of a: 4

Enter the value of b: 6

The Maximum value of two numbers: 6

Using sort() method:

The sort() method sorts the list ascending by default. It also finds the greatest of two numbers.

Example Program:

a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
lst=[a,b]
lst.sort()
print("The maximum value of two numbers is ",lst[-1])

Output:

Enter the value of a: 4

Enter the value of b: 6

The maximum value of two numbers is 6

Conclusion:

In conclusion, Python programming offers compact methods for finding the maximum of two numbers.

Thank you for visiting our site!!!!!……

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top