# Check Average of Odd Elements or Even Elements are Greater in Java

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to find the sum of all even and odd numbers in a given array and compare them to see which one is greater.

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {14, 49, 55,67, 72, 82}


After finding average of sum of even and odd, result will be −

Average of even numbers are: 56.0
Average of odd numbers are: 57.0
Average of odd elements is greater.


#### Instance-2

Suppose the original array is {17, 49, 12, 36, 25, 56}


After finding average of sum of even and odd, result will be −

Average of even numbers are: 34.0
Average of odd numbers are: 30.0
Average of even elements is greater.


#### Instance-3

Suppose the original array is {7, 29, 32, 86, 22, 51}


After updating the array with its next element − {22, 33, 44, 55, 11}

Average of even numbers are: 46.0
Average of odd numbers are: 29.0
Average of even elements is greater.


### Algorithm

• Step 1 − Declare and initialize an integer array.

• Step 2 − Take a for loop and inside it check for the condition of even and odd.

• Step 3 − Also perform addition of counted even and odd.

• Step 4 − Check if the sum of even is greater or sum of odd is greater.

• Step 5 − Print the result.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length


Where ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Array

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check the average of odd elements or even elements are greater.

public class Main{
//main method
public static void main(String args[]){

//Declare and initialize the array elements
int arr[]={ 14, 49, 55,67, 72, 82 };

//get the length of the array
int n = arr.length;
int oddSum=0, evenSum=0, evenCount=0, oddCount=0;

//logic implementation for performing sum of odd and even
for(int i=0; i<n; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
evenCount++;
} else {
oddSum=oddSum+arr[i];
oddCount++;
}
}

//logic implementation for getting average
double avgOdd=oddSum/oddCount;
double avgEven=evenSum/evenCount;
System.out.println("Average of even numbers are: "+avgEven);
System.out.println("Average of odd numbers are: "+avgOdd);
if(avgEven > avgOdd){

//printing the result for even average
System.out.println("\nAverage of even elements is greater.");
} else {

//printing the result for odd average
System.out.println("Average of odd elements is greater.");
}
}
}


### Output

Average of even numbers are: 56.0
Average of odd numbers are: 57.0
Average of odd elements is greater.


## Approach-2: By Using User Defined Method

### Example

In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as a parameter and inside method as per the algorithm to check the average of odd elements or even elements is greater.

public class Main{
//main method
public static void main(String args[]){

//Declare and initialize the array elements
int arr[]={44, 44, 55, 66, 72, 80};

//calling user defined method
avg(arr);
}

//declaring user defined method
public static void avg(int []arr){

//get the length of the array
int n = arr.length;
int oddSum=0, evenSum=0, evenCount=0, oddCount=0;

//logic implementation for performing sum of odd and even
for(int i=0; i<n; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
evenCount++;
} else {
oddSum=oddSum+arr[i];
oddCount++;
}
}

//logic implementation for getting average
double avgOdd=oddSum/oddCount;
double avgEven=evenSum/evenCount;
System.out.println("Average of even numbers are: "+avgEven);
System.out.println("Average of odd numbers are: "+avgOdd);
if(avgEven > avgOdd){
//printing the result for even average
System.out.println("Average of even elements is greater.");
} else {

//printing the result for odd average
System.out.println("Average of odd elements is greater.");
}
}
}


### Output

Average of even numbers are: 61.0
Average of odd numbers are: 55.0
Average of even elements is greater.


In this article, we explored how to check the average of odd elements or even elements are greater by using Java programming language.