Python Program to Find Largest of 3 Numbers

1. Introduction

Determining the largest number out of a set is a basic computational task and an essential part of decision-making processes in programming. Python provides several methods to achieve this, from using conditional statements to built-in functions.

2. Problem Statement

Create a Python program that finds the largest number among three given numbers, without using built-in functions like max.

3. Solution Steps

1. Take three numbers as input.

2. Compare the first number with the second and third numbers.

3. Determine the largest number using conditional statements.

4. Print the largest number found.

4. Code Program

# Define the function to find the largest number among three
def find_largest(num1, num2, num3):
    # Compare each number
    if (num1 >= num2) and (num1 >= num3):
        largest = num1
    elif (num2 >= num1) and (num2 >= num3):
        largest = num2
    else:
        largest = num3
    return largest

# Given numbers
number1 = 15
number2 = 27
number3 = 12

# Call the function to find the largest number
largest_number = find_largest(number1, number2, number3)

# Print the largest number
print(f"The largest number among {number1}, {number2}, {number3} is: {largest_number}")

Output:

The largest number among 15, 27, 12 is: 27

Explanation:

1. find_largest is a function that takes three arguments, num1, num2, and num3.

2. It uses nested conditional if-elif-else statements to compare the numbers.

3. The if condition checks if num1 is greater than or equal to both num2 and num3. If true, num1 is the largest.

4. The elif condition similarly checks if num2 is the largest.

5. The else condition concludes that if neither num1 nor num2 is the largest, then num3 must be.

6. The function returns the largest number, which is stored in largest_number.

7. The print statement outputs the result using an f-string to format the message, indicating that among the given numbers, 27 is the largest.

Comments