-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsecond_largest.py
More file actions
31 lines (26 loc) · 921 Bytes
/
second_largest.py
File metadata and controls
31 lines (26 loc) · 921 Bytes
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
# Program to find the second largest number in a list of n numbers without using sort() or sorted()
# Input the number of elements
n = int(input("Enter the number of elements (n): "))
# Check if n is at least 2
if n < 2:
print("Please enter at least 2 numbers to find the second largest.")
else:
# Input the numbers
numbers = []
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
numbers.append(num)
# Initialize max1 and max2
max1 = max2 = float('-inf')
# Iterate through the numbers to find max1 and max2
for num in numbers:
if num > max1:
max2 = max1
max1 = num
elif num > max2 and num != max1:
max2 = num
# Output the result
if max2 == float('-inf'):
print("All numbers are the same; there is no second largest number.")
else:
print(f"The second largest number is: {max2}")