- 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

# Find if sum of odd or even array elements are smaller 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 elements which is even and sum of elements which is odd in an array and compare their sum and check which one is smaller.

A number is said to be even if it is divisible by 2 else it is called an odd number.

**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 {2, 3, 7, 11, 5, 6, 8, 14}

After comparing the sum of even and odd the result will be −

Sum of Even Numbers:30 Sum of Odd Numbers:26 Sum of odd numbers is smaller

#### Instance-2

Suppose the original array is {12, 23, 11, 64}

After comparing the sum of even and odd the result will be −

Sum of Even Numbers:76 Sum of Odd Numbers:34 Sum of odd numbers is smaller

#### Instance-3

Suppose the original array is {11, 22, 33, 44, 55}

After comparing the sum of even and odd the result will be −

Sum of Even Numbers:66 Sum of Odd Numbers:99 Sum of Even number is smaller

### Algorithm

**Step 1**− Declare and initialize an integer array.**Step 2**− Check for condition of even and odd.**Step 3**− Find sum of even numbers and sum of odd numbers using for loop.**Step 4**− Check whether the sum of even is smaller or the sum of odd is smaller using the if else.**Step 5**− Print the desired 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) { int sumEven = 0, sumOdd = 0; //Declare and initialize the array elements int a[] = {11, 22, 33, 44, 55}; //finding length of an array int n = a.length; //logic implementation for(int i = 0; i < n; i++){ if(a[i] % 2 == 0){ //sum of even numbers sumEven = sumEven + a[i]; } else { //sum of odd numbers sumOdd = sumOdd + a[i]; } } // printing sum of even and odd numbers System.out.println("Sum of Even Numbers:"+sumEven); System.out.println("Sum of Odd Numbers:"+sumOdd); //checking if sum of even is smaller or sum of odd is smaller if(sumEven > sumOdd){ System.out.println("Sum of odd numbers is smaller"); } else { System.out.println("Sum of Even number is smaller"); } } }

### Output

Sum of Even Numbers:66 Sum of Odd Numbers:99 Sum of Even number is smaller

## 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 parameter and inside the method as per the algorithm check the sum of odd numbers or even numbers are smaller.

public class Main { //main method public static void main(String[] args) { //Declare and initialize the array elements int a[] = { 2 , 3, 7, 11, 5, 6, 8, 14 }; //call the user defined method comapre(a); } //user defined method public static void comapre(int []a){ int sumEven = 0, sumOdd = 0; //get the length of the array int n = a.length; //logic implementation for(int i = 0; i < n; i++){ if(a[i] % 2 == 0){ //sum of even numbers sumEven = sumEven + a[i]; } else { //sum of odd numbers sumOdd = sumOdd + a[i]; } } // printing sum of even and odd numbers System.out.println("Sum of Even Numbers:"+sumEven); System.out.println("Sum of Odd Numbers:"+sumOdd); //checking if sum of even is smaller or sum of odd is smaller if(sumEven > sumOdd){ System.out.println("Sum of odd numbers is smaller"); } else { System.out.println("Sum of Even number is smaller"); } } }

### Output

Sum of Even Numbers:30 Sum of Odd Numbers:26 Sum of odd numbers is smaller

In this article, we explored how to check the sum of odd numbers or even numbers are smaller by using Java programming language.

- Related Articles
- Check Average of Odd Elements or Even Elements are Greater in Java
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Determining sum of array as even or odd in JavaScript
- Find elements of an Array which are Odd and Even using STL in C++
- Total possible ways of making sum of odd even indices elements equal in array in \nJavaScript
- Java Program to check if count of divisors is even or odd
- Java Program to Find Sum of First N Odd numbers and Even numbers
- Count number of even and odd elements in an array in C++
- Find Number of Array Elements Smaller than a Given Number in Java
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Java program to find whether given number is even or odd
- How to find the Odd and Even numbers in an Array in java?
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Check if count of divisors is even or odd in Python