
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check 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.