Java Program to Find the Difference between the Sum of Even and Odd Elements in an Array

In the previous article, we have seen Java Program to Print the Elements of an Array Present in Odd Position

In this article we are going to find the difference of sum of even elements and sum of odd elements in an array.

Java Program to Find the Difference between the Sum of Even and Odd Elements in an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the difference of sum of even elements and sum of odd elements.

Method-1: Java Program to Find the Difference between the Sum of Even and Odd Elements in an Array By Static Initialization of Array Elements

Approach:

  • Take an array of elements.
  • Display the array to the user.
  • Use a for loop to traverse through each array element and then check if the element is odd or even.
  • If the element is odd then add it to the oddSum else to the evenSum variable.
  • Print the difference between the sum.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 99, 23, 57, 9};
        // Display the array
        printArray(arr);
        
        int oddSum = 0 , evenSum = 0;
        // Calculates the sum of even and odd elements
        for(int i = 0;i<arr.length;i++)
        {
            if(arr[i]%2==0)
                evenSum+=arr[i];
            else
                oddSum+=arr[i];
        }
        System.out.println("The difference between odd elements sum and even elements sum is "+(oddSum-evenSum));
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        System.out.println("The array elements are");
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements are
12 22 34 22 54 99 23 57 9 
The difference between odd elements sum and even elements sum is 44

Method-2: Java Program to Find the Difference between the Sum of Even and Odd Elements in an Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Display the array to the user.
  • Use a for loop to traverse through each array element and then check if the element is odd or even.
  • If the element is odd then add it to the oddSum else to the evenSum variable.
  • Print the difference between the sum.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        for (int i = 0; i < num; i++) 
        { 
            arr[i] = sc.nextInt(); 
        }
        // Display the array
        printArray(arr);
        
        int oddSum = 0 , evenSum = 0;
        // Calculates the sum of even and odd elements
        for(int i = 0;i<arr.length;i++)
        {
            if(arr[i]%2==0)
                evenSum+=arr[i];
            else
                oddSum+=arr[i];
        }
        System.out.println("The difference between odd elements sum and even elements sum is "+(oddSum-evenSum));
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        System.out.println("The array elements are");
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Enter the number of elements in the array: 5
Enter the elements: 10 11 12 13 14 15
The array elements are
10 11 12 13 14 
The difference between odd elements sum and even elements sum is -12

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: