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:
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
No comments:
Post a Comment