Programming Examples

Python program to input N numbers and then print the second largest number


Write a program to input N numbers and then print the second largest number.

Solution

n = int(input("How many numbers you want to enter? "))
if n > 1 :
    l = int(input())   # Assume first input is largest
    sl = int(input())  # Assume second input is second largest
    if sl > l :
        t = sl
        sl = l
        l = t
    for i in range(n - 2) :
        a = int(input())
        if a > l :
            sl = l
            l = a
        elif a > sl :
            sl = a
    print("Second Largest Number =", sl)
else :
    print("Please enter more than 1 number")
Output

How many numbers you want to enter? 5

55

25

36

12

18

Second Largest Number = 36

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz