How to Find Frequency of Odd & Even Numbers in C++

In this article, we are going to learn about How to Find the Frequency of Odd & Even Numbers in C++. We will take the input from the user for the matrix elements and then we will perform the search for Odd & Even Numbers in the given Matrix.

How to Find Frequency of Odd & Even Numbers in C++?


In this example, we are going to find the Frequency of Odd & Even Numbers in the given Matrix. We will ask the user to enter the values for each index, and once we have the matrices, we will perform the search for Odd & Even Numbers in the given Matrix. We will check each element value one by one to find if it is an even number or an odd number. Let us understand this with the help of the below code example.

#include<iostream>
using namespace std;
int main()
{
	int Matrix[10][10];
	int RowSize , ColumnSize;
	int row, col;
	long EvenElements = 0;
	long OddElements = 0;
	
	
	cout<<"Enter Row and Column Size of the Matrix: ";
    cin>>RowSize>>ColumnSize;

	
	cout<<"Please enter the Elements of the Matrix: ";
    for(row=0; row< RowSize; row++)
    {
        for(col=0; col< ColumnSize; col++)
            cin>> Matrix[row][col];
    }
	
    
    for(row=0; row < RowSize; row++)
    {
        for(col=0; col < ColumnSize; col++)
        {
			if ((Matrix[row][col] % 2) == 0)
                 {
                     ++EvenElements;
                 }
                 else
                     ++OddElements;
        }
    }
						
	cout<<"The Number of Even elements are:  "<<EvenElements<<"\n";
	cout<<"The Number of Odd elements are:  "<<OddElements<<"\n";

    return 0;
}

Output

Enter Row and Column Size of the Matrix: 3
3
Please enter the Elements of the Matrix: 1
2
3
4
5
6
7
8
9
The Number of Even elements are:  4
The Number of Odd elements are:  5

2. How to Find the Frequency of Odd & Even Numbers in C++?


#include<iostream>
using namespace std;
int main()
{
	int Matrix[10][10];
	int RowSize , ColumnSize;
	int row, col;
	long EvenElements = 0;
	long OddElements = 0;
	
	
	cout<<"Enter Row and Column Size of the Matrix: ";
    cin>>RowSize>>ColumnSize;

	
	cout<<"Please enter the Elements of the Matrix: ";
    for(row=0; row< RowSize; row++)
    {
        for(col=0; col< ColumnSize; col++)
            cin>> Matrix[row][col];
    }
	
	
	for(row=0; row < RowSize; row++)
    {
        EvenElements = 0;
		OddElements = 0;
        for(col=0; col < ColumnSize; col++)
        {
			if ((Matrix[row][col] % 2) == 0)
            {
                ++EvenElements;
            }
            else
                ++OddElements;
        }  
        cout<<"The Number of Even elements in Row "<<row+1<< " are:  "<<EvenElements<<"\n";
		cout<<"The Number of Odd elements in Row "<<row+1<< " are: "<<OddElements<<"\n";
    }
						

    for(row=0; row<RowSize; row++)
    {
        EvenElements = 0;
		OddElements = 0;
        for(col=0; col<ColumnSize; col++)
        {
            if ((Matrix[col][row] % 2) == 0)
            {
                ++EvenElements;
            }
            else
                ++OddElements;
        }
        
        cout<<"The Number of Even elements in Column "<<row+1<< " are:  "<<EvenElements<<"\n";
		cout<<"The Number of Odd elements in Column "<<row+1<< " are: "<<OddElements<<"\n";
    }
	
    return 0;
}

Output

Enter Row and Column Size of the Matrix: 3
3
Please enter the Elements of the Matrix: 1
2
3
4
5
6
7
8
9
The Number of Even elements in Row 1 are:  1
The Number of Odd elements in Row 1 are: 2
The Number of Even elements in Row 2 are:  2
The Number of Odd elements in Row 2 are: 1
The Number of Even elements in Row 3 are:  1
The Number of Odd elements in Row 3 are: 2
The Number of Even elements in Column 1 are:  1
The Number of Odd elements in Column 1 are: 2
The Number of Even elements in Column 2 are:  2
The Number of Odd elements in Column 2 are: 1
The Number of Even elements in Column 3 are:  1
The Number of Odd elements in Column 3 are: 2