KnowledgeBoat Logo

Computer Science

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

Python

Python Control Flow

39 Likes

Answer

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

Answered By

11 Likes


Related Questions