Java Array: Sum of Elements in Even-Odd Positions
Arrays are fundamental data structures, and mastering their manipulation opens avenues for efficient programming. In this blog post, we’ll explore the Java programmatic approach to finding the sum of elements in even-odd positions within an array. Follow along for a comprehensive guide, practical coding examples, and insights into effective array traversal.
Understanding the Challenge:
Calculating the sum of elements in even and odd positions involves iterating through an array and selectively adding elements based on their index.
As we know that array index starts from Zero (0) means their 1st element position is the 0 and 5th element position is the 4 and so on.

Before going to the program, you have to know that how to find even and odd numbers: Number is Even or Odd without modulus? after finding the position of the element based on odd/even you can sum the elements. Below program follows the modulus operator to find the even and odd numbers. this is also one of the learning.
Sum of Elements in Even-Odd Positions
public class EvenOddPositionSum {
public static void main(String[] args) {
// Declare and initialize an array
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Calculate sum of elements in even and odd positions
int evenPositionSum = 0;
int oddPositionSum = 0;
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) {
// Even position
evenPositionSum += array[i];
} else {
// Odd position
oddPositionSum += array[i];
}
}
// Display the results
System.out.println("Array: " + Arrays.toString(array));
System.out.println("Sum of elements in even positions: " + evenPositionSum);
System.out.println("Sum of elements in odd positions: " + oddPositionSum);
}
}Output:
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sum of elements in even positions: 25
Sum of elements in odd positions: 30Key Steps in Calculating Sum of Elements:
- Initialize the Array: Declare and initialize the array.
- Iterate Through the Array: Use a loop to traverse through each element.
- Check Even/Odd Position: Use the modulus operator to determine even or odd position.
- Calculate Sums: Accumulate the sum separately for even and odd positions.
- Display Results: Showcase the original array and the calculated sums.
Learn more about Array and their different use cases in Java: Array
Putting it All Together:
Finding the sum of elements in even and odd positions enhances your array manipulation skills. This knowledge becomes invaluable when dealing with data that follows a specific pattern.
Experiment with different arrays, observe the results, and gain confidence in navigating arrays in Java. This skill serves as a foundation for more complex data processing tasks. Happy coding!
Latest posts
- Rotate an Array by K Positions (Left and Right)Title: Rotate an Array by K Positions (Left and Right) Array rotation is one of those small, deceptively simple problems… Read more: Rotate an Array by K Positions (Left and Right)
- Reverse an Array in Groups of KTitle: Reverse an Array in Groups of K Reversing an array in groups of K is a classic interview question… Read more: Reverse an Array in Groups of K
- Reverse Half of an ArrayTitle: Reverse Half of an Array In this guide, you’ll learn how to reverse the first half of an integer… Read more: Reverse Half of an Array
- Reverse an Array using RecursionTitle: Reverse an Array using Recursion Reversing an array is one of the first problems every Java developer meets, and… Read more: Reverse an Array using Recursion
- Reverse an Array using a LoopTitle: Reverse an Array using a Loop Reversing an array is one of the first array manipulations every Java developer… Read more: Reverse an Array using a Loop
- Product of Even and Odd Elements in an ArrayTitle: Product of Even and Odd Elements in an Array In this guide, we will solve a classic beginner-friendly array… Read more: Product of Even and Odd Elements in an Array



