CBSE Class 11 Computer Science Question 139 of 161

Flow of Control — Question 16

Back to all questions
16
Question

Question 16

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
Answer