C program to find the sum of the indices of the elements in the array that contain positive even values. Print out only this sum.
Answers
Explanation:
Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6
Input : arr[] = {15, 12, 13, 10}
Output : 50
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
/* C++ Program to find sum of elements
in a given array */
#include <bits/stdc++.h>
using namespace std;
// function to return sum of elements
// in an array of size n
int sum(int arr[], int n)
{
int sum = 0; // initialize sum
// Iterate through all elements
// and add them to sum
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Driver code
int main()
{
int arr[] = {12, 3, 4, 15};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of given array is " << sum(arr, n);
return 0;
}
// This code is contributed by tushar jindal