Find elements

Java code to count even and odd numbers of an array

Java code to count even and odd Numbers of an array

In this tutorial, we will discuss  the concept of Java code to count even and odd numbers of an array

In this post, we are going to learn how to count the even and odd numbers from the given array.

Java code to count even and odd elements of an array

First, we must understand how to identify odd and even numbers

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers.

 

In my previous post, I have explained the various approaches to check whether the  number is even or odd using Java language

 

here, we will discuss how to count odd and even numbers from an array in Java programming language

 

There are three programs given below

The common procedures of these programs

  • Initially, Declare an integer type array with maximum capacity(100) and declare variables i, size, odd and even
  • The user will enter the size of the array and store in the variable size to define the array size
  • Then, the user enters all the elements of the array according to the array size using the loop
  • After that, use a loop for iterating elements of the array
  • Finally, the following  loops(for, while,do-while) count even and odd number using if statements from the array

Count even and odd numbers in an array using for loop

Program 1

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will count the even and odd numbers from this array using the for loop in Java

 

import java.util.Scanner;
class EvenOddcount{
public static void main (String args[]){
    int even=0,odd=0;
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the array size :\n");

int size=scan.nextInt();//reads input from user for array size
System.out.print("Enter the elements of the array :\n");

int arr[]=new int[size];

for(int i=0; i<arr.length; i++){
   System.out.printf("Enter the element arr[%d]:",i);
        arr[i]=scan.nextInt();//reads input from user for array elements
    }
    for(int i=0; i<size; i++){
        if(arr[i]%2==0){
            even++;
     
        }
        else{
            odd++;
    }
}
System.out.println("Total even numbers of ann array: "+even);//display even numbers
System.out.println("Total even numbers of ann array: "+odd);//display odd numbers
}
}

 

When the above code is executed, it produces the following results

Enter the array size
10
Enter elements of the array:

Enter the element arr[0] : 46
Enter the element arr[1] : 74
Enter the element arr[2] : 79
Enter the element arr[3] : 92
Enter the element arr[4] : 15
Enter the element arr[5] : 52
Enter the element arr[6] : 37
Enter the element arr[7] : 88
Enter the element arr[8] : 57
Enter the element arr[9] : 74

Total even numbers of an array :6
Total odd numbers of an array : 4

 

Count even and odd numbers in an array using while loop

Program 1

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will count the even and odd numbers from this array using the while loop in Java

 

import java.util.Scanner;
class EvenOddcount1{
public static void main (String args[]){
    int even=0,odd=0;
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the array size :\n");

int size=scan.nextInt();
System.out.print("Enter the elements of the array :\n");

int arr[]=new int[size];

int i=0;
while( i<arr.length){
   System.out.printf("Enter the element arr[%d]:",i);
        arr[i]=scan.nextInt();
         i++;
    }
    
    i=0; 
    while(i<size){
        if(arr[i]%2==0){
            even++;
     
        }
        else{
            odd++;
    }
     i++;
}
System.out.println("Total even numbers of ann array: "+even);//display even numbers
System.out.println("Total even numbers of ann array: "+odd);//display odd numbers
}
}

 

When the above code is executed, it produces the following results

Enter the array size
8
Enter elements of the array:

Enter the element arr[0] : 34
Enter the element arr[1] : 54
Enter the element arr[2] : 67
Enter the element arr[3] : 88
Enter the element arr[4] : 95
Enter the element arr[5] : 25
Enter the element arr[6] : 54
Enter the element arr[7] : 44

Total even numbers of an array :5
Total odd numbers of an array : 3

 

Count even and odd numbers in an array using the do-while loop

Program 1

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will count the even and odd numbers from this array using the do-while loop in Java

 

import java.util.Scanner;
class EvenOddcount2{
public static void main (String args[]){
    int even=0,odd=0;
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the array size :\n");

int size=scan.nextInt();
System.out.print("Enter the elements of the array :\n");

int arr[]=new int[size];

int i=0;
do{
   System.out.printf("Enter the element arr[%d]:",i);
        arr[i]=scan.nextInt();
         i++;
    }while( i<arr.length);
    
    i=0; 
    do{
        if(arr[i]%2==0){
            even++;
     
        }
        else{
            odd++;
    }
     i++;
}while(i<size);

System.out.println("Total even numbers of ann array: "+even);//display even numbers
System.out.println("Total even numbers of ann array: "+odd);//display odd numbers
}
}

 

When the above code is executed, it produces the following results

Enter the array size
8
Enter elements of the array:

Enter the element arr[0] : 567
Enter the element arr[1] : 435
Enter the element arr[2] : 788
Enter the element arr[3] : 433
Enter the element arr[4] : 466
Enter the element arr[5] : 144
Enter the element arr[6] : 567
Enter the element arr[7] : 542

Total even numbers of an array :4
Total odd numbers of an array : 4

 

Similar post

C++ code to count even and odd numbers of an array

Java code to compute even and odd numbers of an array

C code to compute even and odd numbers of an array

Display even and odd numbers without if statement in C++

Display even and odd numbers without if statement in Java

Display even and odd numbers without if statement in C

C program to find the largest among three numbers using function

Python code to find the largest of three numbers using the function

C++ code to find the largest of three numbers using the function

Java code to find the largest of three numbers

C code to find the largest of three numbers

C++ code to find the largest of three numbers

Python code to find the largest of three numbers

Java program to find the middle of three number using Method

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

Python code to find the middle of three number using the function

Java code to find middle of three numbers

C code to find middle of three numbers

C++ code to find middle of three numbers

Python code to find middle of three numbers

Java code to find largest of three numbers in an array

Java code to find smallest of three numbers in an array

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

 

Suggested for you

The operator in C++ language

If statement in C++ language

Nested if statement in C++ language

Data type in C++ language

Variable in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

The operator in Python language

If statement in Python language

function in Python

Data types and Variable in Python

for loop in Python language

while loop in Python language

 

Datatype and variables in Java programming language

Operator in Java language

If statements in Java language

Method in Java language

do-while loop in Java

Single dimension array in Java

 

C program to count even and odd numbers in an array
Count even and odd numbers of an array in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

15 hours ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago