Wednesday 25 March 2015

Even and odd element in matrix

Question: Program to find the number of even and odd elements in the matrix.

In this type of questions where we have to find the number of element we use a counting variable e.g count who count the number of occurrence. In this type of matrix questions first of all we declare the order of matrix after the matrix are ready we count the number of occurrence  of the element according to the given condition.

Code snippet :

  1. using System;  
  2. public class findEle  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int i, j, row, col, odd=0, even=0;  
  7.         int[,] matA = new int[10,10];  
  8.       
  9.         Console.Write("Enter the no. of row: ");  
  10.         row=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         col=Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the element in the matrix: ");  
  16.         for(i=0; i<row; i++)  
  17.         {  
  18.             for(j=0; j<col; j++)  
  19.             {  
  20.                 Console.Write("Matrix [{0},{1}] : ", i,j);  
  21.                 matA[i,j] = Convert.ToInt16(Console.ReadLine());  
  22.             }  
  23.   
  24.                 Console.WriteLine();  
  25.         }  
  26.   
  27.             Console.ReadLine();  
  28.   
  29.         Console.WriteLine("\nMatrix: ");  
  30.         for(i=0; i<row; i++)  
  31.         {  
  32.             for(j=0; j<col; j++)  
  33.             {  
  34.                 Console.Write("{0} ",matA[i,j]);  
  35.             }  
  36.   
  37.                 Console.WriteLine();  
  38.         }  
  39.   
  40.             Console.ReadLine();  
  41.   
  42.         for(i=0; i<row; i++)  
  43.         {  
  44.             for(j=0; j<col; j++)  
  45.             {  
  46.                 if(matA[i,j]%2 == 0)  
  47.                     even++;  
  48.                 else  
  49.                     odd++;  
  50.             }  
  51.               
  52.         }  
  53.   
  54.             Console.WriteLine("Total even numbers in matrix is:{0} ", even);  
  55.             Console.WriteLine("Total odd numbers in matrix is: {0}", odd);  
  56.             Console.ReadLine();  
  57.     }  
** Complete code
Description:
In the above code first of all we ask user to input the order of matrix after that we enter the element of the matrix and after that we print the matrix on the console after that we setup the logic of counting number of occurrence of even and odd elements of the matrix.

  1.  for(i=0; i<row; i++)  
  2.         {  
  3.             for(j=0; j<col; j++)  
  4.             {  
  5.                 if(matA[i,j]%2 == 0)  
  6.                     even++;  
  7.                 else  
  8.                     odd++;  
  9.             }  
  10.               
  11.         } 
in the above code we declare two variable even and odd and according to the question there is one increment on even when there is a even element in the matrix same for the odd element of the matrix.

Screenshot of output:


Output of evenodd matrix

Read also:- 

Sum of diagonal of resultant marix
VARARGS
Array in C#