Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Python Tips & Tricks»Python Program to Find the Largest Among Three Numbers

    Python Program to Find the Largest Among Three Numbers

    By RahulJune 25, 20232 Mins Read

    Python, a versatile and powerful programming language, allows us to perform numerous tasks with ease, such as comparing numbers and determining the largest among them. In this article, we’ll walk through creating a Python program that finds the largest among three numbers.

    Python’s control flow statements, especially the if statement, make it simple to compare three numbers and determine which is the largest. Let’s first look at the basic logic, then move on to constructing our program.

    Basic Logic

    The logic for finding the largest among three numbers (let’s call them a, b, and c) goes as follows:

    1. First, we assume that a is the largest number.
    2. We then compare a with b and c. If a is less than either b or c, it’s not the largest number.
    3. If b is greater than a, we now consider b to be the largest number.
    4. We then compare b with c. If b is less than c, then c is the largest number.
    5. If none of the above conditions are true, a remains the largest number.

    Python Program

    Let’s now translate this logic into a Python program.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # Input three numbers
    a = float(input("Enter first number: "))
    b = float(input("Enter second number: "))
    c = float(input("Enter third number: "))
     
    # Assume the first number is the largest
    largest = a
     
    # If the second number is larger than the current largest, update largest
    if (b > largest):
        largest = b
     
    # If the third number is larger than the current largest, update largest
    if (c > largest):
        largest = c
     
    print("The largest number is", largest)

    In this code, the input() function is used to accept the user input. This function returns a string, so we convert it to a float using the float() function. This way, the program can handle not only integers but also real numbers.

    Next, we initially set largest to `a`. Then, we compare largest with `b` and `c` using if statements, updating largest whenever we find a number that’s greater.

    Finally, the program prints the largest number.

    This program works well and is straightforward, but it does not handle cases where the input may not be a number, or if there are less or more than three numbers. In a more comprehensive program, you may want to add error handling to account for these scenarios.

    Conclusion

    Python’s readability and expressiveness make it an excellent choice for simple tasks like finding the largest of three numbers. As we’ve seen, the if statement is a powerful tool that lets us easily compare numbers and other data types. While our program is simple, the concepts it demonstrates are fundamental to programming in Python, and understanding them will serve as a strong foundation for more complex programs and operations.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Create and Use Custom Python Module

    Performance Optimization with Pylint in Python

    Understanding Case Sensitivity in Python

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.