Given a 2D array, print the even numbers of it using Python

Given a 2D array, print the even numbers of it using Python

In this blog post we are taking two integers as input on one line separated by space representing rows and columns of the matrix. Following lines after that will be elements of the matrix with each element separated by space and then print the even elements of the matrix with each element separated by space using python.

 

 

# Given a 2D array, print the even numbers of it using Python
r, c = list(map(int, input().split()))
mat = []
result = []
for i in range(r):
a = list(map(int, input().split()))
mat.append(a)
for i in mat:
for j in i:
if j % 2 == 0:
result.append(j)
print(' '.join(map(str, result)))




No comments

Powered by Blogger.