B.Sc Computer Science with Data Analytics
Python Programming - Lab
Core Lab 5
Bharathiar University
Program 3 - Write a python program that asks user to enter series of positive numbers (the user should enter a negative number to signal the end of series) and the program should display numbers in order and their sum
Source Code
# Initializing an empty list to store positive numbers
numbers = []
# Loop to take input from the user
while True:
num = int(input("Enter a positive number (or a negative number to end the series): "))
if num < 0:
break # Break the loop if a negative number is entered
numbers.append(num)
# Displaying the entered numbers
if len(numbers) > 0:
print("Entered numbers:", sorted(numbers))
# Calculating and displaying the sum of the entered numbers
print("Sum of the numbers:", sum(numbers))
else:
print("No positive numbers were entered.")
numbers = []
# Loop to take input from the user
while True:
num = int(input("Enter a positive number (or a negative number to end the series): "))
if num < 0:
break # Break the loop if a negative number is entered
numbers.append(num)
# Displaying the entered numbers
if len(numbers) > 0:
print("Entered numbers:", sorted(numbers))
# Calculating and displaying the sum of the entered numbers
print("Sum of the numbers:", sum(numbers))
else:
print("No positive numbers were entered.")
OUTPUT:
Enter a positive number (or a negative number to end the series): 5
Enter a positive number (or a negative number to end the series): 6
Enter a positive number (or a negative number to end the series): 3
Enter a positive number (or a negative number to end the series): 16
Enter a positive number (or a negative number to end the series): 10
Enter a positive number (or a negative number to end the series): -1
Entered numbers: [3, 5, 6, 10, 16]
Sum of the numbers: 40
No comments:
Post a Comment