# Sum of even numbers at even position

Here given code implementation process.

``````// C Program
// Sum of even numbers at even position
#include <stdio.h>
// Display array elements
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
void evenAtEvenLocation(int arr[], int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
printArr(arr, n);
printf(" Result : %d \n", sum);
}
int main()
{
// Sorted Arrays
int a[] = {
3 , 1 , 2 , 1 , 6
};
int b[] = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = sizeof(a) / sizeof(a[0]);
int l2 = sizeof(b) / sizeof(b[0]);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0     [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0       [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
evenAtEvenLocation(b, l2);
return 0;
}``````

#### Output

``````  3  1  2  1  6
Result : 8
6  1  1  -3  -2  0  1  2
Result : 4``````
``````/*
Java Program
Sum of even numbers at even position
*/
public class EvenSum
{
// Display array elements
public void printArr(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
public void evenAtEvenLocation(int[] arr, int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
printArr(arr, n);
System.out.print(" Result : " + sum + " \n");
}
public static void main(String[] args)
{
// Sorted Arrays
int[] a =  {
3 , 1 , 2 , 1 , 6
};
int[] b =  {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = a.length;
int l2 = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum of even numbers at even position
*/
class EvenSum
{
public:
// Display array elements
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
void evenAtEvenLocation(int arr[], int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this->printArr(arr, n);
cout << " Result : " << sum << " \n";
}
};
int main()
{
// Sorted Arrays
int a[] = {
3 , 1 , 2 , 1 , 6
};
int b[] = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = sizeof(a) / sizeof(a[0]);
int l2 = sizeof(b) / sizeof(b[0]);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
return 0;
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````// Include namespace system
using System;
/*
Csharp Program
Sum of even numbers at even position
*/
public class EvenSum
{
// Display array elements
public void printArr(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
public void evenAtEvenLocation(int[] arr, int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this.printArr(arr, n);
Console.Write(" Result : " + sum + " \n");
}
public static void Main(String[] args)
{
// Sorted Arrays
int[] a = {
3 , 1 , 2 , 1 , 6
};
int[] b = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = a.Length;
int l2 = b.Length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````package main
import "fmt"
/*
Go Program
Sum of even numbers at even position
*/

// Display array elements
func printArr(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
fmt.Print("\n")
}
func evenAtEvenLocation(arr[] int, n int) {
var sum int = 0
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for i := 0 ; i < n ; i++ {
if (i % 2) == 0 && (arr[i] % 2) == 0 {
sum += arr[i]
}
}
printArr(arr, n)
fmt.Print(" Result : ", sum, " \n")
}
func main() {

// Sorted Arrays
var a = [] int { 3 , 1 , 2 , 1 , 6 }
var b = [] int { 6 , 1 , 1 , -3 , -2 , 0 , 1 , 2 }
// Get the size
var l1 int = len(a)
var l2 int = len(b)
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
evenAtEvenLocation(a, l1)
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
evenAtEvenLocation(b, l2)
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````<?php
/*
Php Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
public	function printArr(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
echo("\n");
}
public	function evenAtEvenLocation(\$arr, \$n)
{
\$sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (\$i = 0; \$i < \$n; ++\$i)
{
if ((\$i % 2) == 0 && (\$arr[\$i] % 2) == 0)
{
\$sum += \$arr[\$i];
}
}
\$this->printArr(\$arr, \$n);
echo(" Result : ".\$sum.
" \n");
}
}

function main()
{
// Sorted Arrays
\$a = array(3, 1, 2, 1, 6);
\$b = array(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
\$l1 = count(\$a);
\$l2 = count(\$b);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
main();``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````/*
Node JS Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
printArr(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
evenAtEvenLocation(arr, n)
{
var sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (var i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this.printArr(arr, n);
process.stdout.write(" Result : " + sum + " \n");
}
}

function main()
{
// Sorted Arrays
var a = [3, 1, 2, 1, 6];
var b = [6, 1, 1, -3, -2, 0, 1, 2];
// Get the size
var l1 = a.length;
var l2 = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
main();``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````#    Python 3 Program
#    Sum of even numbers at even position
class EvenSum :
#  Display list elements
def printArr(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

def evenAtEvenLocation(self, arr, n) :
sum = 0
i = 0
#  This loop are visiting the list element from 0 to n-1.
#  And sum of elements which are exist in Even position.
while (i < n) :
if ((i % 2) == 0 and(arr[i] % 2) == 0) :
sum += arr[i]

i += 1

self.printArr(arr, n)
print(" Result : ", sum ," ")

def main() :
#  Sorted Arrays
a = [3, 1, 2, 1, 6]
b = [6, 1, 1, -3, -2, 0, 1, 2]
#  Get the size
l1 = len(a)
l2 = len(b)
#  Test A
#  arr = [0, 1, 0,1]
#  --------------------------
#   index  Value
#     0      [3]  Index even but value not even
#     ➀     [1]
#     ➁     [2]  Index even and value is even
#     ➂     [1]
#     ➃     [6]  Index even and value is even  [6]
#  ---------------
#   Total :  [2] + [6] = 8
#  Test B
#  arr = [2, 1, 1, -2, 1, 0, 1, 2]
#  --------------------------
#    Index    Value
#     0        [6]     Index even and value is even
#     ➀       [1]
#     ➁       [1]     Index even but value not even
#     ➂       [-3]
#     ➃       [-2]    Index even and value is even
#     ➄       [0]
#     ➅       [1]     Index even but value not even
#     ➆       [2]
#  ---------------
#   Total : [6] + [-2]

if __name__ == "__main__": main()``````

#### Output

``````  3  1  2  1  6
Result :  8
6  1  1  -3  -2  0  1  2
Result :  4``````
``````#    Ruby Program
#    Sum of even numbers at even position
class EvenSum
#  Display array elements
def printArr(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

print("\n")
end

def evenAtEvenLocation(arr, n)
sum = 0
i = 0
#  This loop are visiting the array element from 0 to n-1.
#  And sum of elements which are exist in Even position.
while (i < n)
if ((i % 2) == 0 && (arr[i] % 2) == 0)
sum += arr[i]
end

i += 1
end

self.printArr(arr, n)
print(" Result : ", sum ," \n")
end

end

def main()
#  Sorted Arrays
a = [3, 1, 2, 1, 6]
b = [6, 1, 1, -3, -2, 0, 1, 2]
#  Get the size
l1 = a.length
l2 = b.length
#  Test A
#  arr = [0, 1, 0,1]
#  --------------------------
#   index  Value
#     0      [3]  Index even but value not even
#     ➀     [1]
#     ➁     [2]  Index even and value is even
#     ➂     [1]
#     ➃     [6]  Index even and value is even  [6]
#  ---------------
#   Total :  [2] + [6] = 8
#  Test B
#  arr = [2, 1, 1, -2, 1, 0, 1, 2]
#  --------------------------
#    Index    Value
#     0        [6]     Index even and value is even
#     ➀       [1]
#     ➁       [1]     Index even but value not even
#     ➂       [-3]
#     ➃       [-2]    Index even and value is even
#     ➄       [0]
#     ➅       [1]     Index even but value not even
#     ➆       [2]
#  ---------------
#   Total : [6] + [-2]
end

main()``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
``````
``````/*
Scala Program
Sum of even numbers at even position
*/
class EvenSum()
{
// Display array elements
def printArr(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
def evenAtEvenLocation(arr: Array[Int], n: Int): Unit = {
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr(i) % 2) == 0)
{
sum += arr(i);
}
i += 1;
}
printArr(arr, n);
print(" Result : " + sum + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: EvenSum = new EvenSum();
// Sorted Arrays
var a: Array[Int] = Array(3, 1, 2, 1, 6);
var b: Array[Int] = Array(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
var l1: Int = a.length;
var l2: Int = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````
``````import Foundation;
/*
Swift 4 Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
func printArr(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func evenAtEvenLocation(_ arr: [Int], _ n: Int)
{
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
i += 1;
}
self.printArr(arr, n);
print(" Result : ", sum ," ");
}
}
func main()
{
// Sorted Arrays
let a: [Int] = [3, 1, 2, 1, 6];
let b: [Int] = [6, 1, 1, -3, -2, 0, 1, 2];
// Get the size
let l1: Int = a.count;
let l2: Int = b.count;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}
main();``````

#### Output

``````  3  1  2  1  6
Result :  8
6  1  1  -3  -2  0  1  2
Result :  4``````
``````/*
Kotlin Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
fun printArr(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
fun evenAtEvenLocation(arr: Array < Int > , n: Int): Unit
{
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
i += 1;
}
this.printArr(arr, n);
print(" Result : " + sum + " \n");
}
}
fun main(args: Array < String > ): Unit
{
// Sorted Arrays
val a: Array < Int > = arrayOf(3, 1, 2, 1, 6);
val b: Array < Int > = arrayOf(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
val l1: Int = a.count();
val l2: Int = b.count();
// Test A
// arr = [0, 1, 0,1]
// --------------------------
//  index  Value
//    0      [3]  Index even but value not even
//    ➀     [1]
//    ➁     [2]  Index even and value is even
//    ➂     [1]
//    ➃     [6]  Index even and value is even  [6]
// ---------------
//  Total :  [2] + [6] = 8
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
//   Index    Value
//    0        [6]     Index even and value is even
//    ➀       [1]
//    ➁       [1]     Index even but value not even
//    ➂       [-3]
//    ➃       [-2]    Index even and value is even
//    ➄       [0]
//    ➅       [1]     Index even but value not even
//    ➆       [2]
// ---------------
//  Total : [6] + [-2]
}``````

#### Output

`````` 3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.