Exercise 3

Write a Python algorithm which asks the user to enter two numbers 'a' and 'b' and display their maximum without using the max() function.

Solution

# ask the user to enter two numbers 'a' and 'b'
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))

# we compare the values of 'a' and 'b'
if a > b:
    print("The maximum of", a, "and", b, "is", a)
else:
    print("The maximum of", a, "and", b, "is", b)

In this program:

  1. We ask the user: to enter two numbers a and b using the input() function.
  2. The numbers 'a' and 'b': are stored as floats in the variables a and b.
  3. We use the if statement: to compare the values of 'a' and 'b'.
  4. If a is greater than b: the program prints a message indicating that the maximum of the two numbers is 'a'.
  5. Otherwise: the program prints a message indicating that the maximum of the two numbers is 'b'.




 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 3: python algorithm to find the maximum of two numbers”

Leave a Reply