Sunday, February 10, 2019

Summer Vaccation Homework (Q8)

Q. Write a program that takes any two lists L and M of the same size and adds their elements together to form a new list N whose elements are the sums of their corresponding elements in L and M. (for instance if L = [3, 1, 4] and M = [1, 5, 9], then N should be equal to [4, 6, 13])

Code:

n = int(input("Enter number of terms: "))
L = [0] * n
M = [0] * n
N = [0] * n
print("Enter", n, "elements for list L:")
for i in range(n):
L[i] = int(input("Element " + str(i+1) + ": "))
print("Enter", n, "elements for list M:")
for i in range(n):
M[i] = int(input("Element " + str(i+1) + ": "))
for i in range(n):
N[i] = L[i] + M[i]
print("List L:", L)
print("List M:", M)
print("List N:", N)
view raw addList01.py hosted with ❤ by GitHub

No comments: