This is a program to find the frequency of odd & even numbers in the given matrix.
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.
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.
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.
/*
* C program to find the frequency of odd numbers
* and even numbers in the input of a matrix
*/
#include <stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n, even = 0, odd = 0;
printf("Enter the order ofthe matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefficients of matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if ((array[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", array[i][j]);
}
printf("\n");
}
printf("\n The frequency of occurrence of odd number = %d \n", odd);
printf("The frequency of occurrence of even number = %d\n", even);
}
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.
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 Books in C Programming, Data Structures and Algorithms.
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Practice BCA MCQs