All submissions

Print Sum of Even Numbers

Code

#include <stdio.h>

int sum_even_numbers(int *ptr, int n) {
    // Your logic here
    int sum = 0;                    // intilize sum variable with zero
    for(int i=0;i<n;i++){           // iterate the loop n times
        if(*ptr % 2 == 0){          // check the condition *ptr value divisible by zero or not with 2
            sum += *ptr;            // adding the *ptr value with sum
        }
           *ptr++;                  // increment the ptr
    }
    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

 

int sum_even_numbers(int *ptr, int n) {
    // Your logic here
    int sum = 0;                    // intilize sum variable with zero
    for(int i=0;i<n;i++){           // iterate the loop n times
        if(*ptr % 2 == 0){          // check the condition *ptr value divisible by zero or not with 2
            sum += *ptr;       // adding the *ptr value with sum
        }
       *ptr++;                     // increment the ptr
    }
    return sum;
}

 

Loading...

Input

5 10 21 32 43 50

Expected Output

Sum = 92