Given an array of size n. The problem is to find the sum of numbers which are even and are at even index.

**Examples:**

Input : arr[] = {5, 6, 12, 1, 18, 8} Output : 30 Explanation: Here, n = 6 Now here are index and numbers as: index->arr[index] 0->5, 1->6, 2->12, 3->1, 4->18, 5->8 so, number which are even and are at even indices are: 2->12, 4->18 sum = 12+18 = 30 Input : arr[] = {3, 20, 17, 9, 2, 10, 18, 13, 6, 18} Output : 26 Explanation: Here, n = 10 Now here are index and numbers as: index->arr[index] 0->3, 1->20, 2->17, 3->9, 4->2, 5->10, 6->18, 7->13, 8->6, 9->18 So, number which are even and are at even indices are: 4->2, 6->18, 8->6 sum = 2+18+6 = 26

## C++

`// C++ implementation to ` `// find sum of even numbers ` `// at even indices ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate sum ` `// of even numbers at even indices ` `int` `sum_even_and_even_index( ` ` ` `int` `arr[], ` `int` `n) { ` ` ` ` ` `int` `i = 0, sum = 0; ` ` ` ` ` `// calculating sum of even ` ` ` `// number at even index ` ` ` `for` `(i = 0; i < n; i+=2) { ` ` ` ` ` `if` `(arr[i] % 2 == 0) { ` ` ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `int` `main() { ` ` ` `int` `arr[] = {5, 6, 12, 1, 18, 8}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Sum of even numbers at even indices is "` ` ` `<< sum_even_and_even_index(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find sum of ` `// even numbers at even indices ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum ` ` ` `// of even numbers at even indices ` ` ` `static` `int` `sum_even_and_even_index( ` ` ` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `int` `i = ` `0` `, sum = ` `0` `; ` ` ` ` ` `// calculating sum of even ` ` ` `// number at even index ` ` ` `for` `(i = ` `0` `; i < n; i+=` `2` `) { ` ` ` ` ` `if` `(arr[i] % ` `2` `== ` `0` `) { ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `arr[] = {` `5` `, ` `6` `, ` `12` `, ` `1` `, ` `18` `, ` `8` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(` `"Sum of even numbers"` ` ` `+ ` `" at even indices is "` `+ ` ` ` `+ sum_even_and_even_index(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# python 3 implementation to ` `# find sum of even numbers ` `# at even indices ` ` ` `# Function to calculate sum ` `# of even numbers at even indices ` `def` `sum_even_and_even_index(arr,n): ` ` ` ` ` `i ` `=` `0` ` ` `sum` `=` `0` ` ` `# calculating sum of even ` `# number at even index ` ` ` `for` `i ` `in` `range` `(` `0` `,n,` `2` `): ` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `0` `) : ` ` ` `sum` `+` `=` `arr[i] ` ` ` ` ` `# required sum ` ` ` `return` `sum` ` ` ` ` `# Driver program to test above ` `arr ` `=` `[` `5` `, ` `6` `, ` `12` `, ` `1` `, ` `18` `, ` `8` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Sum of even numbers at "` `, ` ` ` `"even indices is "` `, ` ` ` `sum_even_and_even_index(arr, n)) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find sum of ` `// even numbers at even indices ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum ` ` ` `// of even numbers at even indices ` ` ` `static` `int` `sum_even_and_even_index( ` ` ` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `int` `i = 0, sum = 0; ` ` ` ` ` `// calculating sum of even ` ` ` `// number at even index ` ` ` `for` `(i = 0; i < n; i+=2) { ` ` ` ` ` `if` `(arr[i] % 2 == 0) { ` ` ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` `int` `[]arr = {5, 6, 12, 1, 18, 8}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(` `"Sum of even numbers"` ` ` `+ ` `" at even indices is "` `+ ` ` ` `+ sum_even_and_even_index(arr, n)); ` ` ` `} ` `} ` ` ` `//This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to ` `// find sum of even numbers ` `// at even indices ` ` ` `// Function to calculate sum ` `// of even numbers at even indices ` `function` `sum_even_and_even_index(` `$arr` `, ` `$n` `) ` `{ ` ` ` ` ` `$i` `= 0; ` `$sum` `= 0; ` ` ` ` ` `// calculating sum of even ` ` ` `// number at even index ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `=` `$i` `+2) { ` ` ` ` ` `if` `(` `$arr` `[` `$i` `] % 2 == 0) { ` ` ` ` ` `$sum` `+= ` `$arr` `[` `$i` `]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `{ ` ` ` `$arr` `= ` `array` `(5, 6, 12, 1, 18, 8); ` ` ` `$n` `= sizeof(` `$arr` `) / sizeof(` `$arr` `[0]); ` ` ` `echo` `"Sum of even numbers at "` `. ` ` ` `"even indices is "` `, ` ` ` `sum_even_and_even_index(` `$arr` `, ` `$n` `); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum of even numbers at even indices is 30

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:

- Reverse a subarray of the given array to minimize the sum of elements at even position
- Even numbers at even index and odd numbers at odd index
- Sort an array without changing position of negative numbers
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Maximum path sum for each position with jumps under divisibility condition
- Position of Elements which are equal to sum of all Preceding elements
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Sum of cubes of first n even numbers
- Sum of elements till the smallest index such that there are no even numbers to its right
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Range Queries for finding the Sum of all even parity numbers
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Check if N numbers with Even Sum can be selected from a given Array
- Count of permutations such that sum of K numbers from given range is even
- Minimum array size after repeated replacement of even sum pair with sum
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Rearrange array such that all even-indexed elements in the Array is even

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.