All submissions

Print Sum of Even Numbers

Code

// 40. Print Sum of Even Numbers
// You are given an array of integers and its size. Using only pointer arithmetic:

// Traverse the array
// Find the sum of all even numbers
// Print the sum 
// ❌ Do not use arr[ i ] indexing.
// ✅ Use only pointer movement and dereferencing.

#include <stdio.h>

int sum_even_numbers(int *ptr, int n) {
    // Your logic here
    int result = 0; 

    while(n >0){
         
        if (((*ptr)%2) == 0){
            result += *ptr; 
        }
        ++ptr;
        n--; 
    }

    return result;
}

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

 

 

 

Loading...

Input

5 10 21 32 43 50

Expected Output

Sum = 92