July 14, 2021, Learn eTutorial

1032

Before learning has to count a number of even and odd elements in an array, let's start with what is odd and even number. An odd number is a number that is divisible by **2**, and a number that is not divisible by two is called an **even** number. We can check a number is odd or even in the c language using a mod operator by **2**. If the remainder is zero, then the number is an **even **number. Else the number is **odd**.

Example: Check 5 and 8 are Odd or Even.

**5 % 2 = 1**.So**5**is**Odd**number.**8 % 2 = 0**.So**8**is**Even**number.

In this c program, we need to check and the frequency of odd numbers & even numbers in a given matrix. So we need to analyze each element in the matrix to check whether it is odd or even. So here in this c program, we first need to accept the matrix order from the user, then we get the elements of the matrix. Then we display the given matrix using the nested `for loop`

.

Now we start a `for loop`

and check each element of the matrix by using the **mod **operator by two and check the remainder is zero or not. If it is zero, increment the even counter by one, else increment the odd one. Display the counter value for odd and even numbers as a result.

The syntax of for loop is given by:

```
for (initializationStatement; testExpression; updateStatement)
{
// codes
}
```

Here the initialization statement is executed only once. Initially, we evaluate the test expression. If the test expression is false, we will terminate the loop. But if the test expression is true, then the code inside the `for loop`

is executed and updated. This process continues until the test expression is False. This type of loop is commonly used when the number of iterations is already known.

**STEP 1:** Include the header files to use the built-in functions in the C program.

**STEP 2: **Declare the integer variables** i, j, m, n, a, even, odd**, and Matrix **ma[][]** and set Even and **Odd=0**.

**STEP 3:** Read the Order of the Matrix into **m **and **n**.

**STEP 4:** Read the coefficients of the Matrix from the user using nested `for loop`

and saved it into **ma[i][j]**.

**STEP 5:** If **ma[i][j]%2=0** then increment **Even **by **one**.

**STEP 6:** Else increment **Odd **by **one**.

**STEP 7:** Repeat step 4.

**STEP 8:** Display the given Matrix as **ma[i][j] **by using nested `for loop`

.

**STEP 9:** Display the frequency of occurrence of an Odd number as Odd.

**STEP 10:** Display The frequency of occurrence of the Even number as Even.

` ````
#include <stdio.h>
void main() {
static int ma[10][10];
int i, j, m, n, even = 0, odd = 0;
printf("Enter the order of the matrix \n");
scanf("%d %d", & m, & n);
printf("Enter the coefficients if matrix \n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
scanf("%d", & ma[i][j]);
if ((ma[i][j] % 2) == 0) {
++even;
} else
++odd;
}
}
printf("The given matrix is\n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", ma[i][j]);
}
printf("\n");
}
printf("\nThe frequency of occurrence of odd number = %d\n", odd);
printf("The frequency of occurrence of even number = %d\n", even);
} /* End of main() */
```

Enter the order of the matrix 3 3 Enter the coefficients of the matrix 1 2 3 4 5 6 7 8 9 The given matrix is 1 2 3 4 5 6 7 8 9 The frequency of occurrence of odd number = 5 The frequency of occurrence of even number = 4