In this python tutorial. I will show you how to find the second number amoung three numbers with two alternative way.
Accept three numbers from the user and display the second largest number in python.
Way 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #Python program to find the second largest of three given numbers num1 = int(input("Enter first number :")) num2 = int(input("Enter second number :")) num3 = int(input("Enter third number :")) if num1 > num2 and num1 > num3: if num2>num3: print("The second largest number is",num2) else: print("The largest number is",num3) elif num2 > num1 and num2 >num3: if num1 > num3: print("The second largest number is", num1) else: print("The second largest number is", num3) else: if num1 > num2: print("The second largest number is",num1) else: print("The second largest number is", num2) |
Output:
1 2 3 4 5 6 | Enter first number :10 Enter second number :20 Enter third number :30 The second largest number is 20 |
This is a Python program that finds the second largest number out of three given numbers.
The program starts by prompting the user to enter three numbers using the input() function and storing them in the variables num1, num2, and num3. The input() function returns a string, so the program uses the int() function to convert the strings to integers.
The program then uses a series of if-elif-else statements to compare the values of the three numbers and determine the second largest number. The program checks if num1 is greater than num2 and num3, and if so, it checks if num2 is greater than num3. If that is the case, it prints “The second largest number is” and the value of num2. If num3 is greater than num2, the program prints “The second largest number is” and the value of num3.
The program then uses elif statement to check if num2 is greater than num1 and num3, and if so, it checks if num1 is greater than num3. If that is the case, it prints “The second largest number is” and the value of num1. If num3 is greater than num1, the program prints “The second largest number is” and the value of num3.
Finally, the program uses an else statement to check if num3 is greater than num1 and num2. If that is the case, it checks if num1 is greater than num2. If that is true, it prints “The second largest number is” and the value of num1. If num2 is greater than num1, it prints “The second largest number is” and the value of num2.
In summary, this program prompts the user to enter three numbers, and then uses a series of if-elif-else statements to compare the values of the three numbers and determine the second largest number, it prints the result on the screen.
Way 2:
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 26 27 28 29 30 31 | #Python program to find the second largest of three given numbers num1 = int(input("Enter first number :")) num2 = int(input("Enter second number :")) num3 = int(input("Enter third number :")) largest=0 smallest=0 #find largest number if num1 > num2 and num1 > num3: largest = num1 elif num2 > num1 and num2 >num3: largest = num2 else: largest = num3 #find smallest number if num1 < num2 and num1 < num3: smallest = num1 elif num2 < num1 and num2 < num3: smallest = num2 else: smallest = num3 secondLargest = num1+num2+num3 - (largest + smallest) print("The second Largest Number:",secondLargest) |
Output:
1 2 3 4 5 6 | Enter first number :10 Enter second number :20 Enter third number :30 The second largest number is 20 |
This is a Python program that finds the second largest number out of three given numbers.
The program starts by prompting the user to enter three numbers using the input() function and storing them in the variables num1, num2, and num3. The input() function returns a string, so the program uses the int() function to convert the strings to integers.
The program then declares two variables, largest and smallest, and initializes them with zero.
The program then uses a series of if-elif-else statements to compare the values of the three numbers and determine the largest and smallest number. The program checks if num1 is greater than num2 and num3, and if so, assigns the value of num1 to the variable largest. If num2 is greater than num1 and num3, it assigns the value of num2 to the variable largest. If num3 is greater than num1 and num2, it assigns the value of num3 to the variable largest. The same process is repeated to find the smallest number.
Then the program calculates the second largest number by subtracting the largest and smallest number from the sum of the three numbers.
Finally, the program prints “The second Largest Number: ” and the value of the variable secondLargest.
In summary, this program prompts the user to enter three numbers, uses a series of if-elif-else statements to compare the values of the three numbers and determine the largest and smallest number, and then calculates the second largest number by subtracting the largest and smallest number from the sum of the three numbers and prints the result on the screen.