Find frequencies of even and odd numbers in a matrix

Here given code implementation process.

/*
  C Program 
  Find frequencies of even and odd numbers in a matrix
*/
#include <stdio.h>
#define ROW 3
#define COL 5

//Display matrix elements
void show_data(int matrix[][COL])
{
  for (int i = 0; i < ROW; ++i)
  {
    for (int j = 0; j < COL; ++j)
    {
      printf("%3d",matrix[i][j] );
    }
    printf("\n");
  }
  printf("\n");
}

void frequencies(int matrix[ROW][COL])
{
  //Counter variable which are store the information 
  //About number of even and odd elements
  int even=0,odd=0;

  for (int i = 0; i < ROW; ++i)
  {
    for (int j = 0; j < COL; ++j)
    {
      if(matrix[i][j]%2==0)
      {
        //When even element
        even++;
      }
      else
      {
        //When get new odd number
        odd++;
      }
    }
  }
  //Display the number of even and odd elements
  printf("  Even Elements : %d\n",even );
  printf("  Odd Elements: %d\n",odd );


}
int main(){

  int matrix[ROW][COL] = {  
    { 1, 2, 3, 4, 5 }, 
    { 4, 5, 6 , 11 , 12 }, 
    { 7, 8, 9 ,13, 14} 
  }; 
  show_data(matrix);
  frequencies(matrix);            
   

  return 0;
}

Output

  1  2  3  4  5
  4  5  6 11 12
  7  8  9 13 14

  Even Elements : 7
  Odd Elements: 8
/*
  C++ Program
  Find frequencies of even and odd numbers in a matrix
*/
#include<iostream>
#define ROW 3
#define COL 5
using namespace std;

class MyMatrix {
  public:
  //Display matrix elements
  void show_data(int matrix[][COL])
  {
    for (int i = 0; i < ROW; ++i)
    {
      for (int j = 0; j < COL; ++j)
      {
         cout<<"  "<<matrix[i][j];
      }
       cout<<"\n";
    }
     cout<<"\n";
  }

  void frequencies(int matrix[ROW][COL])
  {
    //Counter variable which are store the information 
    //About number of even and odd elements
    int even=0,odd=0;

    for (int i = 0; i < ROW; ++i)
    {
      for (int j = 0; j < COL; ++j)
      {
        if(matrix[i][j]%2==0)
        {
          //When even element
          even++;
        }
        else
        {
          //When get new odd number
          odd++;
        }
      }
    }
    //Display the number of even and odd elements
    cout<<"  Even Elements : "<<even <<endl;
    cout<<"  Odd Elements: "<<odd <<endl;


  }

};
int main() {
  MyMatrix obj;
  int matrix[][COL] = {  
    { 1, 2, 3, 4, 5 }, 
    { 4, 5, 6 , 11 , 12 }, 
    { 7, 8, 9 ,13, 14} 
  }; 
  obj.show_data(matrix);
  obj.frequencies(matrix);    


  return 0;
}

Output

  1  2  3  4  5
  4  5  6  11  12
  7  8  9  13  14

  Even Elements : 7
  Odd Elements: 8
/*
  Java Program
  Find frequencies of even and odd numbers in a matrix
*/
public class MyMatrix {
  
  //Display matrix elements
  public void show_data(int [][]matrix)
  {
    //Get the size of matrix
    int row = matrix.length;
    int col = matrix[0].length;
    for (int i = 0; i < row; ++i)
    {
      for (int j = 0; j < col; ++j)
      {
        System.out.print("  "+matrix[i][j] );
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }

  public void frequencies(int [][]matrix)
  {
    //Counter variable which are store the information 
    //About number of even and odd elements
    int even=0,odd=0;
    //Get the size of matrix
    int row = matrix.length;
    int col = matrix[0].length;
    for (int i = 0; i < row; ++i)
    {
      for (int j = 0; j < col; ++j)
      {
        if(matrix[i][j]%2==0)
        {
          //When even element
          even++;
        }
        else
        {
          //When get new odd number
          odd++;
        }
      }
    }
    //Display the number of even and odd elements
    System.out.print("  Even Elements : "+even+" \n" );
    System.out.print("  Odd Elements:  "+odd+"\n" );
  }
  public static void main(String[] args) {
    MyMatrix obj = new MyMatrix();
    //Define matrix 
    int [][]matrix = {  
      { 1, 2, 3, 4, 5 }, 
      { 4, 5, 6 , 11 , 12 }, 
      { 7, 8, 9 ,13, 14} 
    }; 
    obj.show_data(matrix);
    obj.frequencies(matrix);            
   
  }
}

Output

  1  2  3  4  5
  4  5  6  11  12
  7  8  9  13  14

  Even Elements : 7
  Odd Elements: 8
using System;
/*
  C# Program
  Find frequencies of even and odd numbers in a matrix
*/

public class MyMatrix {
	//Display matrix elements
	public void show_data(int[,] matrix) {
		//Get the size of matrix
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public void frequencies(int[,] matrix) {
		//About number of even and odd elements
		//Counter variable which are store the information 
		//About number of even and odd elements
		int even = 0, odd = 0;
		//Get the size of matrix
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				if (matrix[i,j] % 2 == 0) {
					//When even element
					even++;
				} else {
					//When get new odd number
					odd++;
				}
			}
		}
		Console.Write(" Even Elements : " + even + " \n");
		Console.Write(" Odd Elements: " + odd + "\n");
	}
	public static void Main(String[] args) {
		MyMatrix obj = new MyMatrix();
		//Define matrix 
      	int[,] matrix = {
			{
				1,
				2,
				3,
				4,
				5
			},
			{
				4,
				5,
				6,
				11,
				12
			},
			{
				7,
				8,
				9,
				13,
				14
			}
		};
		obj.show_data(matrix);
		obj.frequencies(matrix);
	}
}

Output

 1 2 3 4 5
 4 5 6 11 12
 7 8 9 13 14

 Even Elements : 7
 Odd Elements: 8
<?php
/*
  Php Program
  Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
	//Display matrix elements

	public 	function show_data($matrix) {
		//Get the size of matrix
		$row = count($matrix);
		$col = count($matrix[0]);
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
	public 	function frequencies($matrix) {
		//Counter variable which are store the information 
		//About number of even and odd elements
		$even = 0;
		$odd = 0;
		//Get the size of matrix
		$row = count($matrix);
		$col = count($matrix[0]);
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				if ($matrix[$i][$j] % 2 == 0) {
					//When even element
					$even++;
				} else {
					//When get new odd number
					$odd++;
				}
			}
		}
		//Display the number of even and odd elements

		echo(" Even Elements : ". $even ." \n");
		echo(" Odd Elements: ". $odd ."\n");
	}
};

function main() {
	$obj = new MyMatrix();
	//Define matrix 
	$matrix = array(array(1, 2, 3, 4, 5), array(4, 5, 6, 11, 12), array(7, 8, 9, 13, 14));
	$obj->show_data($matrix);
	$obj->frequencies($matrix);

}
main();

Output

 1 2 3 4 5
 4 5 6 11 12
 7 8 9 13 14

 Even Elements : 7
 Odd Elements: 8
/*
 Node Js Program
 Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
	//Display matrix elements
	show_data(matrix) {
		//Get the size of matrix
		var row = matrix.length;
		var col = matrix[0].length;
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}

		process.stdout.write("\n");
	}
	frequencies(matrix) {
		//Counter variable which are store the information 
		//About number of even and odd elements
		var even = 0;
		var odd = 0;
		//Get the size of matrix
		var row = matrix.length;
		var col = matrix[0].length;
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				if (matrix[i][j] % 2 == 0) {
					//When even element
					even++;
				} else {
					//When get new odd number
					odd++;
				}
			}
		}

		//Display the number of even and odd elements

		process.stdout.write(" Even Elements : " + even + " \n");
		process.stdout.write(" Odd Elements: " + odd + "\n");
	}
}

function main(args) {
	var obj = new MyMatrix();
	//Define matrix 
	var matrix = [
		[1, 2, 3, 4, 5],
		[4, 5, 6, 11, 12],
		[7, 8, 9, 13, 14]
	];
	obj.show_data(matrix);
	obj.frequencies(matrix);
}
main();

Output

 1 2 3 4 5
 4 5 6 11 12
 7 8 9 13 14

 Even Elements : 7
 Odd Elements: 8
# Python 3 Program
# Find frequencies of even and odd numbers in a matrix
class MyMatrix :
	# Display matrix elements
	def show_data(self, matrix) :
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		while (i < row) :
			j = 0
			while (j < col) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print(end = "\n")
			i += 1
		
		print(end = "\n")
	
	def frequencies(self, matrix) :
		even = 0
		odd = 0
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		while (i < row) :
			j = 0
			while (j < col) :
				if (matrix[i][j] % 2 == 0) :
					# When even element
					even += 1
				else :
					# When get new odd number
					odd += 1
				
				j += 1
			
			i += 1
		
		print(" Even Elements : ", even ," \n", end = "")
		print(" Odd Elements: ", odd ,"\n", end = "")
	

def main() :
	obj = MyMatrix()
	matrix = [
		[1, 2, 3, 4, 5],
		[4, 5, 6, 11, 12],
		[7, 8, 9, 13, 14]
	]
	obj.show_data(matrix)
	obj.frequencies(matrix)


if __name__ == "__main__":
	main()

Output

  1  2  3  4  5
  4  5  6  11  12
  7  8  9  13  14

 Even Elements :  7
 Odd Elements:  8
# Ruby Program 
# Find frequencies of even and odd numbers in a matrix
class MyMatrix 
	# Display matrix elements
	def show_data(matrix) 
		row = matrix.length
		col = matrix[0].length
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
	def frequencies(matrix) 
		even = 0
		odd = 0
		row = matrix.length
		col = matrix[0].length
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				if (matrix[i][j] % 2 == 0) 
					# When even element
					even += 1
				else 
					# When get new odd number
					odd += 1
				end
				j += 1
			end
			i += 1
		end
		print(" Even Elements  :", even ," \n")
		print(" Odd Elements :", odd ,"\n")
	end
end
def main() 
	obj = MyMatrix.new()
	matrix = [
		[1, 2, 3, 4, 5],
		[4, 5, 6, 11, 12],
		[7, 8, 9, 13, 14]
	]
	obj.show_data(matrix)
	obj.frequencies(matrix)
end
main()

Output

 1 2 3 4 5
 4 5 6 11 12
 7 8 9 13 14

 Even Elements  :7 
 Odd Elements :8
/*
 Scala Program
 Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
	//Display matrix elements
	def show_data(matrix: Array[Array[Int]]): Unit = {
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
	def frequencies(matrix: Array[Array[Int]]): Unit = {
		var even: Int = 0;
		var odd: Int = 0;
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				if (matrix(i)(j) % 2 == 0) {
					//When even element
					even += 1;
				} else {
					//When get new odd number
					odd += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print(" Even Elements : " + even + " \n");
		print(" Odd Elements: " + odd + "\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyMatrix = new MyMatrix();
		val matrix: Array[Array[Int]] = Array(
			Array(1, 2, 3, 4, 5),
			Array(4, 5, 6, 11, 12),
			Array(7, 8, 9, 13, 14));
		obj.show_data(matrix);
		obj.frequencies(matrix);
	}
}

Output

 1 2 3 4 5
 4 5 6 11 12
 7 8 9 13 14

 Even Elements : 7
 Odd Elements: 8
/*
  Swift 4 Program
  Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
	//Display matrix elements
	func show_data(_ matrix: [
		[Int]
	]) {
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
	func frequencies(_ matrix: [
		[Int]
	]) {
		var even: Int = 0;
		var odd: Int = 0;
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				if (matrix[i][j] % 2 == 0) {
					//When even element
					even += 1;
				} else {
					//When get new odd number
					odd += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print(" Even Elements : ", even ," \n", terminator: "");
		print(" Odd Elements: ", odd ,"\n", terminator: "");
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	let matrix: [
		[Int]
	] = [
		[1, 2, 3, 4, 5],
		[4, 5, 6, 11, 12],
		[7, 8, 9, 13, 14]
	];
	obj.show_data(matrix);
	obj.frequencies(matrix);
}
main();

Output

  1  2  3  4  5
  4  5  6  11  12
  7  8  9  13  14

 Even Elements :  7
 Odd Elements:  8


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved