Java Program to find the sum of even and odd elements in the array


In this program, You will learn how to find even and odd elements in an array in java.


List is: 1 2 3 4 5

Sum of even is: 2 + 4 => 6

Sum of odd is: 1 + 3 + 5 => 9

Example: How to find the sum of even and odd elements in an array in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        int i, se = 0, sod = 0;
        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 5 numbers:");

        for (i = 0; i < 5; i++) {
            arr[i] = sc.nextInt();
        }

        for (i = 0; i < 5; i++) {
            if (arr[i] % 2 == 0) {
                se = se + arr[i];
            } else {
                sod = sod + arr[i];
            }
        }

        System.out.println("Sum of even elements:" + se);
        System.out.println("Sum of odd elements:" + sod);

    }
}

Output:

Enter 5 numbers:2 3 4 5 6
Sum of even elements:12
Sum of odd elements:8

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.