Count Even and Odd Numbers in Java

Count even and odd numbers in java. The blue square represents all odd numbers, whereas the red circles represent all even numbers. Loop through the array and verify if each element is divisible by two to find the frequencies of odd and even numbers. Increase the count of count Even by one if it is divisible by two. Otherwise, increase count Odd by one.

public class Main 
public static void main(String[] args)
int rows, cols, countOdd = 0, countEven = 0;
int a[][] = {
{3, 4, 7},
{3, 5, 7},
{8, 2, 6}
rows = a.length;
cols = a[0].length;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(a[i][j] % 2 == 0)
System.out.println("Frequency of odd numbers: " + countOdd);
System.out.println("Frequency of even numbers: " + countEven);



Frequency of odd numbers: 5
Frequency of even numbers: 4

Count Even and Odd Numbers in Java

A number that can be divided into two equal groups is called an even number. A number that cannot be divided into two equal groups is called an odd number. Even numbers, regardless of how many digits they contain, end in 2, 4, 6, 8, and 0; we know the number 5,917,624 is even since it ends in a 4! Odd numbers have endings of 1, 3, 5, 7, and 9.

Count in Java Program

The counting() method counts the number of elements in the stream supplied as a parameter. It returns a Collector that accepts T-type input items and counts the number of them. The result is 0 if no elements are present.

Why Empty Matrix is Declared in Java Program

There are no rows or columns in an empty matrix. A matrix with missing values, like a matrix with zeros, must have at least one row and column.

Matrix in Java Program

Matrix is a type of mathematics that is defined as a two-dimensional array in the shape of a rectangle with elements that are either numbers, symbols, or expressions. The elements of the matrix are arranged in a row and column format. M n matrix refers to a matrix with m rows and n columns. Count even and odd numbers in java for beginners to understand the concept of matrix.

Compiler: Click Here