Write a program to read a list of n integers (positive as well as negative). Create two new lists, one
having all positive numbers and the other having all negative numbers from the given list. Print all three lists.
Answers
The following codes are written in Python.
Source code:
n = int(input("Enter the number of elements you'd like to enter: "))
l = list()
for i in range(n):
x = int(input("Enter the element: "))
l.append(x)
print(l, "is your given list.")
pl = list()
nl = list()
for i in l:
if i > 0:
pl.append(i)
elif i < 0:
nl.append(i)
print(pl, "is the list of positive numbers.")
print(nl, "is the list of negative numbers.")
append() is used for adding elements to a list.
a = int(input("Enter an integer: "))
b = int(input("Enter a second interger: "))
c = int(input("Enter a third integer: "))
s = a + b + c
print()
print(s, "is the sum of the three integers.")
avg = (a + b + c)//3
print(avg, "is the average of the three integers.")
if s > avg:
d = s - avg
print(d, "is the difference between the sum and the average.")
elif avg > s:
d = avg - s
print(d, "is the difference between the sum and the average.")