Find Average of Odd and Even Numbers in Array Java

In this article, you will see how to find the Average of Odd and Even numbers in an Array using Java. To understand this program, you should have the basic knowledge of an integer array and the looping concept.

Even Number: Any number that can be exactly divided by 2 is called an even number.
Odd Number: An odd number is a number that is not divisible by 2.

AVERAGE FORMULA,
Average = Total sum of elements / Count of elements

Algorithm

  1. Declare and Initialize an integer array.
  2. Declare the required variables such as evenSum, evenCount, evenAvg, oddSum, oddCount, and oddAvg.
  3. Iterate the array from the 0th index to array length -1.
  4. Inside the loop, check each array element is an Odd or Even number.
  5. If arr[i] % 2 == 0, then even number otherwise odd number.
  6. In the case of an Even number, increment the evenCount and calculate the evenSum.
  7. In the case of an Odd number, increment the oddCount and calculate the oddSum.
  8. Calculate the average using the average formula.
  9. Finally, print the result.

Example- Calculate the average of Odd and Even numbers in an Array in Java

Let’s see the java program code below:

package com.javacodepoint.array;

public class FindAvgOfOddEvenInArray {

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

		// Declare and Initialize an integer array
		int arr[] = { 2, 4, 3, 8, 11, 50, 7, 15, 5 };

		// Declare and Initialize required variables
		int evenSum = 0, oddSum = 0, evenCount = 0, oddCount = 0;
		float evenAvg = 0.0f, oddAvg = 0.0f;

		// Iterate the array
		for (int i = 0; i < arr.length; i++) {

			// Check whether the No. is an Even or Odd
			// if remainder is 0, it is an Even number otherwise Odd number
			if (arr[i] % 2 == 0) { // Even
				evenCount++;
				evenSum = evenSum + arr[i];
			} else { // Odd
				oddCount++;
				oddSum = oddSum + arr[i];

			}
		}

		// Calculate the average of even numbers
		evenAvg = (float) evenSum / evenCount;

		// Print the result
		System.out.println("Sum of Even numbers in Array= " + evenSum);
		System.out.println("Count of Even numbers in Array= " + evenCount);
		System.out.println("The average of Even numbers= " + evenAvg);

		// Calculate the average of odd numbers
		oddAvg = (float) oddSum / oddCount;

		// Print the result
		System.out.println("Sum of Odd numbers in Array= " + oddSum);
		System.out.println("Count of Odd numbers in Array= " + oddCount);
		System.out.println("The average of Odd numbers= " + oddAvg);

	}

}

OUTPUT:

Sum of Even numbers in Array= 64
Count of Even numbers in Array= 4
The average of Even numbers= 16.0
Sum of Odd numbers in Array= 41
Count of Odd numbers in Array= 5
The average of Odd numbers= 8.2

See also:
Java Program to Find the Largest Number in an Array.
Star pattern programs [Tricks] in java.

Java logical programs list


Java Basic Programs

Java String Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *