
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Find if sum of odd or even array elements are smaller in Java
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Swap Even Index Elements And Odd Index Elements in Python
- Program to check whether elements frequencies are even or not in Python
- Separate Odd and Even Elements into Two Separate Arrays in Java
- Sorting odd and even elements separately JavaScript
- Find Number of Odd and Even Elements in Each Row of a Matrix in Java
- Find Array Elements Which are Greater than its Left Elements in Java?
- Java Program to check if count of divisors is even or odd
- Find elements of an Array which are Odd and Even using STL in C++
- Java Program to Check Whether a Number is Even or Odd
- Count subarrays with same even and odd elements in C++
- Count number of even and odd elements in an array in C++
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- Java program to split the Even and Odd elements into two different lists
