How to Write A Python Function To Find The Max Of Three Numbers

Given the three numbers either integer and floating point numbers,  your task is to write a Python function to pass the free numbers and get the maximum number out of these three values.

The Python function should return the maximum value.

 

 

How To Write A Python Function To Find The Max Of Three Numbers

 


Write A Python Function To Find The Max Of Three Numbers

Let's define a function named find_max() having three numbers as an input argument,  these numbers may be integer or floating point. Just take a variable maximum and set it to the first variable now compare the maximum with second and third using the if statement.   

Upon comparing based on the largest value, set the maximum variable  at greatest. Once the two comparisons are done return that value stored in maximum.

def find_max(a, b, c):
   maximum = a
   if b > maximum:
       maximum = b
   if c > maximum:
       maximum = c
   return maximum




In the main program, take three variables A, B and C and ask user to enter the three integer variables  using input function call and convert them to integers.

a = int(input("Enter a First Number : "))
b = int(input("Enter a Second Number : "))
c = int(input("Enter a Third Number : "))




Take another variable named the greatest,  call the function find_max() by  passing the three values as an input argument to a function. Once the function executes, the greatest variable will store the maximum of three numbers.

Finally print the greatest of three numbers using the print function.

greatest = find_max(a, b, c)
print("Greatest of three numbers is : ", greatest)





Complete Program
=====================

def find_max(a, b, c):
   maximum = a
   if b > maximum:
       maximum = b
   if c > maximum:
       maximum = c
   return maximum


a = int(input("Enter a First Number : "))
b = int(input("Enter a Second Number : "))
c = int(input("Enter a Third Number : "))
greatest = find_max(a, b, c)
print("Greatest of three numbers is : ", greatest)





OUTPUT
======================

Enter a First Number : 66
Enter a Second Number : 23
Enter a Third Number : 90
Greatest of three numbers is :  90




OUTPUT
======================

Enter a First Number : 11
Enter a Second Number : 22
Enter a Third Number : 10
Greatest of three numbers is :  22





Write A Python Function To Find The Max Of Three Floating Point Numbers

The above program can also be written for the Floating point values,  by just modifying the  integer type conversion to the floating point type conversion  as shown by the below program.

def find_max(a, b, c):
   maximum = a
   if b > maximum:
       maximum = b
   if c > maximum:
       maximum = c
   return maximum


a = float(input("Enter a First Number : "))
b = float(input("Enter a Second Number : "))
c = float(input("Enter a Third Number : "))
greatest = find_max(a, b, c)
print("Greatest of three numbers is : ", greatest)




OUTPUT
===========

Enter a First Number : 23.4
Enter a Second Number : 23.5
Enter a Third Number : 23.05
Greatest of three numbers is :  23.5




Conclusion
================

Try to run the program by yourself,  by providing the random three value and note down the output.


Post a Comment

0 Comments