Program in Python to count odd elements of a 2D array

Rajnilari2015
Posted by Rajnilari2015 under Python category on | Points: 40 | Views : 1423
Suppose we have a 2D array

{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
}

We need to find the count of odd elements of the 2D array. The below program will do so

from System import *
from System.Collections.Generic import *
from System.Linq import *

class Program(object):
def Main(args):
oddCounter = 0
row = 4
col = 4
matrix = Array[int]((, , , ))
i = 0
while i < row:
j = 0
while j < col:
if matrix[i][j] % 2 != 0:
oddCounter += 1
j += 1
i += 1
Console.Write("Number of Odd Elements in the matrix : " + oddCounter)
Console.ReadKey()

Main = staticmethod(Main)

Result
--------
Number of Odd Elements in the matrix : 8

Comments or Responses

Login to post response