Prerequisite – Array Basics

Given an array, write a program to find the sum of values of even and odd index positions separately.

Examples:

Input :arr = {1, 2, 3, 4, 5, 6}Output :Even index positions sum 9 Odd index positions sum 12Explanation:Here, n = 6 so there will be 3 even index positions and 3 odd index positions in an array Even = 1 + 3 + 5 = 9 Odd = 2 + 4 + 6 = 12Input :arr = {10, 20, 30, 40, 50, 60, 70}Output :Even index positions sum 160 Odd index positions sum 170Explanation:Here, n = 7 so there will be 3 odd index positions and 4 even index positions in an array Even = 10 + 30 + 50 + 70 = 160 Odd = 20 + 40 + 60 = 120

## C/C++

`// CPP program to find out ` `// Sum of elements at even and ` `// odd index positions separately ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` `// Function to calculate sum ` `void` `EvenOddSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `even = 0; ` ` ` `int` `odd = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// Loop to find even, odd sum ` ` ` `if` `(i % 2 == 0) ` ` ` `even += arr[i]; ` ` ` `else` ` ` `odd += arr[i]; ` ` ` `} ` ` ` ` ` `cout << ` `"Even index positions sum "` `<< even; ` ` ` `cout << ` `"\nOdd index positions sum "` `<< odd; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4, 5, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `EvenOddSum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find out ` `// Sum of elements at even and ` `// odd index positions separately ` `import` `java.io.*; ` ` ` `class` `EvenOddSum { ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `}; ` ` ` `int` `even = ` `0` `, odd = ` `0` `; ` ` ` ` ` `// Loop to find even, odd sum ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) { ` ` ` `if` `(i % ` `2` `== ` `0` `) ` ` ` `even += arr[i]; ` ` ` `else` ` ` `odd += arr[i]; ` ` ` `} ` ` ` ` ` `System.out.println(` `"Even index positions sum: "` `+ even); ` ` ` `System.out.println(` `"Odd index positions sum: "` `+ odd); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find out ` `# Sum of elements at even and ` `# odd index positions separately ` ` ` `# Function to calculate Sum ` `def` `EvenOddSum(a, n): ` ` ` `even ` `=` `0` ` ` `odd ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Loop to find evem, odd Sum ` ` ` `if` `i ` `%` `2` `=` `=` `0` `: ` ` ` `even ` `+` `=` `a[i] ` ` ` `else` `: ` ` ` `odd ` `+` `=` `a[i] ` ` ` ` ` `print` `"Even index positions sum "` `, even ` ` ` `print` `"nOdd index positions sum "` `, odd ` ` ` `# Driver Function ` ` ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `] ` `n ` `=` `len` `(arr) ` ` ` `EvenOddSum(arr, n) ` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find out ` `// Sum of elements at even and ` `// odd index positions separately ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 1, 2, 3, 4, 5, 6 }; ` ` ` `int` `even = 0, odd = 0; ` ` ` ` ` `// Loop to find even, odd sum ` ` ` `for` `(` `int` `i = 0; i < arr.Length; i++) ` ` ` `{ ` ` ` `if` `(i % 2 == 0) ` ` ` `even += arr[i]; ` ` ` `else` ` ` `odd += arr[i]; ` ` ` `} ` ` ` ` ` `Console.WriteLine(` `"Even index positions"` ` ` `+ ` `" sum: "` `+ even); ` ` ` ` ` `Console.WriteLine(` `"Odd index positions "` ` ` `+ ` `"sum: "` `+ odd); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find out ` `// Sum of elements at even and ` `// odd index positions separately ` ` ` `// Function to calculate sum ` `function` `EvenOddSum(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$even` `= 0; ` ` ` `$odd` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Loop to find even, odd sum ` ` ` `if` `(` `$i` `% 2 == 0) ` ` ` `$even` `+= ` `$arr` `[` `$i` `]; ` ` ` `else` ` ` `$odd` `+= ` `$arr` `[` `$i` `]; ` ` ` `} ` ` ` ` ` `echo` `(` `"Even index positions sum "` `. ` `$even` `); ` ` ` `echo` `(` `"\nOdd index positions sum "` `. ` `$odd` `); ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 1, 2, 3, 4, 5, 6 ); ` `$n` `= sizeof(` `$arr` `); ` ` ` `EvenOddSum(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Even index positions sum 9 Odd index positions sum 12

This article is contributed by **Rishabh Jain**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to print product of even and odd indexed elements in an Array
- Even numbers at even index and odd numbers at odd index
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Find Kth element in an array containing odd elements first and then even elements
- C++ program to print all Even and Odd numbers from 1 to N
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Rearrange array such that all even-indexed elements in the Array is even
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Number of permutations such that sum of elements at odd index and even index are equal
- Count subarrays having sum of elements at even and odd positions equal
- Count number of even and odd elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Count number of even and odd length elements in an Array
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Print the lexicographically smallest array by swapping elements whose sum is odd
- Count of odd and even sum pairs in an array