Question
Write a python program to find the largest of three numbers
Hint: Use if and elif
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#Accept three numbers print "Enter the numbers for comparision" nos1 = input("Enter first number: ") nos2 = input("Enter second number: ") nos3 = input("Enter third number: ") #Comparison using if elif clauses if (nos1 > nos2) and (nos1 > nos3): large = nos1 elif (nos2 > nos1) and (nos2 > nos3): large = nos2 else: large = nos3 print "The largest number is",large |
Explanation:
Three numbers are accepted from the user ‘nos1′,’nos2′,’nos3’.
First ‘nos1’ is checked.If it is greater than both ‘nos2’ and ‘nos3’, then ‘large’ is allocated wth the value of ‘nos1’
Otherwise ‘nos2’ is checked. If it is greater than both ‘nos1’ and ‘nos2’,then ‘large’ is allocated with the value of ‘nos2’
Otherwise ‘large’ is allocated with ‘nos3’
Output:
1 2 3 4 5 6 7 8 9 |
Enter the numbers for comparision Enter first number: 89 Enter second number: 23 Enter third number: 10 The largest number is 89 |
Leave a Reply