49. Print Sum of Even Numbers

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int sum_even_numbers(int *ptr, int n) {
    // Your logic here
    int sum = 0;
    for(int i = 0; i<n; i++)
    {
        if((ptr[i])%2==0)
        {
            sum += ptr[i];
        }
        
    }
    return sum;
}

int main() {
    int n;
    scanf("%d", &n);

    int arr[100];
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int result = sum_even_numbers(arr, n);

    printf("Sum = %d", result);

    return 0;
}

Solving Approach

 

1.Loop through array and access each element and check if it is even
2. if so add and store in sum 

3. Finally return the sum

 

Was this helpful?
Upvote
Downvote