Greatest of Three Numbers in Python: Simple Program

Greatest of Three Numbers in Python: Simple Program

Greatest of Three Numbers in Python

Greatest of Three Numbers in Python: Simple Program
    Using an if-else statement.
    Using Python’s built-in max() function.
    Using a custom function

    These methods will help you understand how to approach this problem in different ways, depending on the situation and your programming preferences.


    Input and Output Format

    • Input: The input consists of three integers, entered by the user.
    • Output: The program outputs the greatest number among the three.

    Sample Input:

     
    44
    12
    76

    Sample Output:

     
    76

    Algorithm to Find the Greatest of Three Numbers Using If-Else in Python

    Here is the basic algorithm you will use in the if-else approach:

    1. Input: Get three integer values from the user.
    2. Comparison: Use an if-else statement to compare the numbers.
      • If num1 is greater than or equal to both num2 and num3, print num1.
      • If num2 is greater than num1 and num3, print num2.
      • Otherwise, print num3.
    3. Output: Print the greatest of the three numbers.

    Method 1: Using If-Else Statement

    This is the most manual method of checking which number is the greatest by comparing them one by one.

    Python Code Using If-Else:

    python
    # Python program to find the greatest of three numbers using if-else statement
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    num3 = int(input("Enter the third number: "))
    if (num1 >= num2) and (num1 >= num3):
    greatest = num1
    elif (num2 >= num1) and (num2 >= num3):
    greatest = num2
    else:
    greatest = num3print(“The greatest number is”, greatest)

    Sample Input and Output

    Input:

     
    54
    86
    24

    Output:

     
    86

    Method 2: Using the Max Function

    Python provides a built-in function called max() that returns the largest of the numbers passed to it. This method is compact and easy to use.

    Python Code Using Max Function:

    python
    # Python program to find the greatest of three numbers using max function
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    num3 = int(input("Enter the third number: "))
    print(max(num1, num2, num3), “is the greatest”)

    Sample Input and Output

    Input:

     
    24
    58
    95

    Output:

    csharp
    95 is the greatest

    Method 3: Using Functions

    A custom function allows you to define the logic of finding the greatest number in a more reusable and organized manner. This method is ideal if you plan to check the greatest of three numbers multiple times.

    Python Code Using a Function:

    python
    # Python program to find the greatest of three numbers using functions
    def maximum(a, b, c): # Function Definition
    if (a >= b) and (a >= c):
    largest = a
    elif (b >= c):
    largest = b
    else:
    largest = c
    return largest
    a = int(input(“Enter the first number: “))
    b = int(input(“Enter the second number: “))
    c = int(input(“Enter the third number: “))print(“The greatest number is”, maximum(a, b, c)) # Function Call

    Sample Input and Output

    Input:

     
    256
    24
    123

    Output:

     
    256

    Which Method Should You Use?

    • Method 1 (If-Else Statement) is simple and works well for small, quick scripts. It’s great when you’re learning the fundamentals of conditionals.
    • Method 2 (Max Function) is the most concise and efficient. It minimizes the code and is best for production code when you want to write clean, readable Python code.
    • Method 3 (Custom Function) is useful when you want to make your code reusable and organized. If you need to check the greatest of three numbers in multiple places in your program, using a function is the best approach.

    Conclusion

    In this article, we explored three methods to find the greatest of three numbers in Python:

    1. Using if-else statements for manual comparison.
    2. Using the max() function for a cleaner, built-in solution.
    3. Using a custom function for reusable and modular code.

    Each of these methods has its own use cases, and choosing the right one depends on the context and your preferences. Whether you need to understand basic conditionals or write more efficient Python code, these methods will give you a solid foundation for solving this problem in Python.

    Click Here to know more our program:

    Greatest of Three Numbers in Python

     

    c