Hi, in this article let's learn how to Find Largest of Three Numbers using Python Program.
Write a Python Program to Find Largest of Three Numbers
Lets ask the user to enter three numbers using the input function call. Take three variables: first, second and third variables. Convert them into integers using int function call.
first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))
third = int(input("Enter Third Number : "))
Initialize max as first number. Compare max variable with second using if statement and if the second number is greater than max, set max as second. Again use the if statement and compare the third variable with max and set max value as third if the third integer is greater than max.
After the two if statements get executed the max variable will be storing the max of three integer numbers.
max = first
if second > max:
max = second
if third > max:
max = third
Finally print the max of three numbers using the print statement.
print("Maximum of three numbers is : ", max)
Final Program:
first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))
third = int(input("Enter Third Number : "))
max = first
if second > max:
max = second
if third > max:
max = third
print("Maximum of three numbers is : ", max)
OUTPUT 1:
Enter First Number : 23
Enter Second Number : 24
Enter Third Number : 25
Maximum of three numbers is : 25
OUTPUT 2:
Enter First Number : 300
Enter Second Number : 200
Enter Third Number : 100
Maximum of three numbers is : 300
OUTPUT 3:
Enter First Number : 20
Enter Second Number : 500
Enter Third Number : 20
Maximum of three numbers is : 500
write a python function to find the max of three numbers using function
Define the function and add the same logic, take the largest variable and initialize the large as the first number.
Compare the second number with large, if the second number is maximum set large to the second number.
Compare the third number with large, if the third number is maximum set large to the third number.
def find_max(a, b, c):
large = a
if b > large:
large = b
if c > large:
large = c
return large
first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))
third = int(input("Enter Third Number : "))
max = find_max(first, second, third)
print("Maximum of three numbers is : ", max)
Conclusion: Try to run the program and give the input as random numbers. Comment down if you have any queries / suggestions.
0 Comments