Page 282 - Computer Science V2.0 Class 11
P. 282

03     Objective: To find maximum of three numbers
                04     Inputs:
                05     num1, num2, num3 : Numeric values'''
                06     if num1 < num2:
                07         if num2 < num3:
                08             return num3
                09         else:
                10             return num2
                11     else:
                12         if num1 < num3:
                13             return num3
                14         else:
                15             return num1
                16 n1 = int(input('Enter first number :'))
                17 n2 = int(input('Enter second number :'))
                18 n3 = int(input('Enter third number :'))
                19 print('Maximum of ', n1, n2, n3, ':', max3(n1, n2, n3))

              Output:
               >>> Enter first number :9
               >>> Enter second number :4
               >>> Enter third number :10
                    Maximum of 9 4 10 : 10
              Maximum of Three Numbers (Revisited): Note that the above solution to finding the largest of three numbers is
              somewhat confusing. We can simplify the above code substantially if we first write a function to find the maximum of
              a pair of numbers, as illustrated in Program 10.12:

               Program 10.12 Write a function max2(n1,n2) that finds the maximum of two numbers and then make use of the
               function max2(n1,n2) to write the function max3(n1, n2, n3) to find the maximum of the numbers n1, n2, and n3.

                01 def max2(num1, num2):
                02     '''
                03     Objective: To find maximum of two numbers
                04     Inputs :
                05     num1, num2: Numeric values
                06     Return value: Maximum of num1, num2
                07     '''
                08     if num1 < num2:
                09         return num2
                10     else:
                11         return num1
                12
                13 def max3(num1, num2, num3):
                14     '''
                15     Objective: To find maximum of three numbers
                16     Inputs:
                17     num1, num2, num3 : Numeric values
                18     Return value: Maximum of num1, num2, num3
                19     '''
                20
                21     temp = max2(num1, num2)
                22     return max2(temp, num3)
                23
                24 n1 = int(input('Enter first number :'))
                25 n2 = int(input('Enter second number : '))
                26 n3 = int(input('Enter third number : '))
                27 print('Maximum of', n1, n2, n3, ':', max3(n1, n2, n3))



               268   Touchpad Computer Science (Ver. 2.0)-XI
   277   278   279   280   281   282   283   284   285   286   287