Python

Accept three numbers from the user and display the second largest number in python4 min read

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:

Output:

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:

Output:

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.

Leave a Comment