Python Program to Find Largest Number

Python Program to Find Largest Number
Reading Time: 6 minutes

Introduction:

The program uses the find_largest_number function to determine the largest number among the three given numbers. The get_user_input function ensures that the user provides valid numerical input and handles any input errors gracefully.

When you run the program, it will prompt you to enter three numbers one by one, and then it will display the largest number among them.

Algorithm for the "largest number finder" program:

  1. Define the function find_largest_number(num1, num2, num3) that takes three numbers as input and returns the largest number among them.
  2. Inside the find_largest_number function: a. Check if num1 is greater than or equal to both num2 and num3. b. If the condition is true, return num1 as the largest number. c. Otherwise, check if num2 is greater than or equal to both num1 and num3. d. If the condition is true, return num2 as the largest number. e. If none of the above conditions are met, return num3 as the largest number.
  3. Define the function get_user_input() to get user input for three numbers. a. Inside the function, use try and except to handle any input errors (e.g., non-numeric input). b. Prompt the user to enter the first, second, and third numbers one by one. c. Convert the input to floating-point numbers and return them as a tuple (num1, num2, num3).
  4. In the main section of the program (if __name__ == “__main__”:), run the following steps: a. Print a welcome message to the user. b. Call the get_user_input() function to get three numbers from the user. c. Call the find_largest_number(num1, num2, num3) function with the user-input numbers to find the largest number among them. d. Print the result, showing the largest number to the user.

Python Code for Finding Largest Number :

    
     def find_largest_number(num1, num2, num3):
    if num1 >= num2 and num1 >= num3:
        return num1
    elif num2 >= num1 and num2 >= num3:
        return num2
    else:
        return num3
def get_user_input():
    try:
        num1 = float(input("Enter the first number: "))
        num2 = float(input("Enter the second number: "))
        num3 = float(input("Enter the third number: "))
        return num1, num2, num3
    except ValueError:
        print("Invalid input. Please enter valid numbers.")
        return get_user_input()
if __name__ == "__main__":
    print("Welcome to the largest number finder!")
    num1, num2, num3 = get_user_input()
    largest_number = find_largest_number(num1, num2, num3)
    print(f"The largest number is: {largest_number}")

    
   

Input :

    
     Welcome to the largest number finder!
Enter the first number: 5
Enter the second number: 6
Enter the third number: 18
    
   

Output :

    
     The largest number is: 18.0
    
   

Explanation:

The program starts by defining two functions. The find_largest_number function takes three numbers as input and returns the largest among them using a series of conditional statements (if-elif-else). It compares the first number with the other two and returns the largest one based on the conditions.

The get_user_input function is responsible for taking user input. It uses a try block to handle potential ValueError exceptions that may occur if the user enters invalid input (e.g., non-numeric characters). If there’s an error, it displays a message and recursively calls itself to try getting valid input again. Otherwise, it converts the user’s input to floating-point numbers and returns them as a tuple.

In the main section of the program, it first prints a welcome message. Then, it calls the get_user_input function to get three numbers from the user. After receiving the input, it calls the find_largest_number function with the obtained numbers to find the largest among them. Finally, it prints the result, displaying the largest number to the user.