Java program to calculate sum of even and odd numbers in an array

Let’s learn java program to calculate sum of even and odd numbers in an array.

Java program to calculate sum of even and odd numbers in an array

In the below java program first user enters number of elements in an array or size of an array and also array elements using nextInt() method of Scanner class.

Then again using for loop, loop through the elements of an array. In the meanwhile using if statement and modulus operator check whether remainder of the element divided by 2 is equal to 0 or not.

If the condition is true then the number is an even number else the number is odd number. Here’s the java program to find sum of even and odd numbers in an array using for loop.

import java.util.Scanner;
public class FindSumOfOddEven
{
   public static void main(String[] args)
   {
      int number, sumEven = 0, sumOdd = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in an array: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number];
      System.out.println("Please enter elements of the array: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(int a = 0; a < number; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            sumEven = sumEven + arrNumbers[a];
         }
         else
         {
            sumOdd = sumOdd + arrNumbers[a];
         }
      }
      System.out.println("Sum of even numbers in an array: " + sumEven);
      System.out.println("Sum of odd numbers in an array : " + sumOdd);
      sc.close();
   }
}


Output:

Please enter number of elements in an array: 8
Please enter elements of the array:
1
2
3
4
5
6
7
8
Sum of even numbers in an array: 20
Sum of odd numbers in an array : 16


Now let’s to calculate or find sum of even and odd numbers in an array using while loop. To execute the logic to find sum of even and odd numbers in an array we are using two while loops and if else statement.

import java.util.Scanner;
public class FindSumOfOddEven
{
   public static void main(String[] args) 
   {
      int size, a = 0, b = 0, sumEven = 0, sumOdd = 0;;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in an array: ");
      size = sc.nextInt();
      int[] arrNumbers = new int[size];
      System.out.print("Please enter " + size + " elements of an array: ");
      while(a < size)
      {
         arrNumbers[a] = sc.nextInt();
         a++;
      }
      while(b < size)
      {
         if(arrNumbers[b] % 2 == 0)
         {
            sumEven = sumEven + arrNumbers[b]; 
         }
         else
         {
            sumOdd = sumOdd + arrNumbers[b]; 
         }
         b++;
      }       
      System.out.println("Sum of even numbers in array: " + sumEven);
      System.out.println("Sum of odd numbers in array: " + sumOdd);
      sc.close();
   }
}


Output:

Please enter number of elements in an array:
10
Please enter 10 elements of an array: 1
2
3
4
5
6
7
8
9
10
Sum of even numbers in array: 30
Sum of odd numbers in array: 25

Also read – nested classes in java