In this example, we will discuss the concept of the Python program:find the greatest of three numbers using the function
In this post, we will learn how to find the greatest number of three numbers using a user-defined function in the Python programming language.
Program 1
Find the greatest of three numbers using if elif statements
This program allows the user to enter three numbers and compare to select the largest number using if-elif statements.
Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
num1=int(input("Enter the first number: ")); num2=int(input("Enter the second number: ")); num3=int(input("Enter the Third number: ")); def find_Biggest(): #function definition if(num1>=num2) and (num1>=num2): largest=num1 elif(num2>=num1) and (num2>=num3): largest=num2 else: largest=num3 print("Largest number is",largest) find_Biggest(); #function call |
Output:
1 2 3 4 5 6 |
Enter the first number: 125 Enter the second number: 452 Enter the Third number: 258 Largest number is 452 |
Program 2
Find the greatest of three numbers using nested if statements
This program allows the user to enter three numbers and compare to select the largest number using nested if statements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
num1=int(input("Enter the first number: ")); num2=int(input("Enter the second number: ")); num3=int(input("Enter the Third number: ")); def find_Biggest1():#function definition if(num1>num2): if(num1>num3): largest=num1 else: largest=num3 else: if num2>num3: largest=num2 else: largest=num3 #Display the laegest number print("Largest number is: ",largest) find_Biggest1();#function call |
Output:
1 2 3 4 5 6 |
Enter the first number: 25 Enter the second number: 52 Enter the Third number: 32 Largest number is: 52 |