# C Program to Find the Frequency of Odd & Even Numbers in the given Matrix

«
»

This is a program to find the frequency of odd & even numbers in the given matrix.

Problem Description

This C Program finds frequency of odd & even numbers in the given matrix. The program first accepts the matrix. Then finds the odd and even numbers in a matrix. Then finds the occurrence of odd and even numbers in a given matrix.

Problem Solution

1. Create a matrix of MXN order, define all its elements. Declare variables even and odd to keep track of even and odd numbers
2. Using nested loop, at the time of entering each data, check whether they are divisible by 2 or not.
3. Hence the variable even and odd will get incremented each time an even or odd number is found respectively.

Program/Source Code

Here is source code of the C program to find the frequency of odd & even numbers in the given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

1. `    /*`
2. `     * C program to find the frequency of odd numbers`
3. `     * and even numbers in the input of a matrix`
4. `     */`
5. ` `
6. `    #include <stdio.h>`
7. `    void main()`
8. `    {`
9. ` `
10. `    	static int array;`
11. `    	int i, j, m, n, even = 0, odd = 0;`
12. ` `
13. `    	printf("Enter the order ofthe matrix \n");`
14. `    	scanf("%d %d", &m, &n);`
15. ` `
16. `    	printf("Enter the coefficients of matrix \n");`
17. `    	for (i = 0; i < m; ++i) `
18. `        {`
19. `                for (j = 0; j < n; ++j)`
20. `                {`
21. `                     scanf("%d", &array[i][j]);`
22. `                     if ((array[i][j] % 2) == 0)`
23. `                     {`
24. `                         ++even;`
25. `                     }`
26. `                     else`
27. `                         ++odd;`
28. `                 }`
29. ` `
30. `    	}`
31. ` `
32. `    	printf("The given matrix is \n");`
33. `    	for (i = 0; i < m; ++i)`
34. `        {`
35. `            for (j = 0; j < n; ++j) `
36. `            {`
37. `                printf(" %d", array[i][j]);`
38. `            }`
39. `            printf("\n");`
40. `        }`
41. ` `
42. `        printf("\n The frequency of occurrence of odd number  = %d \n", odd);`
43. `        printf("The frequency of occurrence of even number = %d\n", even);`
44. ` `
45. `    }`
Program Explanation

1. Declare a 2D array (matrix), of some fixed capacity.
2. Take order of the matrix as input from users and define the matrix.
3. Declare two variables even and odd to keep track of respective numbers. Initialize them with 0.
4. Now, using a nested for loop, while reading the elements of array, also check whether the element modulo 2 is zero or not indicating even or odd number respectively.
5. Variable even and odd are incremented by 1 each time when even or odd number is detected.
6. Print the value of even and odd variable to get the frequency of even and odd numbers.

Runtime Test Cases
```Enter the order of the matrix
3 3
Enter the coefficients of matrix
34 36 39
23 57 98
12 39 49
The given matrix is
34 36 39
23 57 98
12 39 49

The frequency of occurrence of odd number  = 5
The frequency of occurrence of even number = 4```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms. 