Untitled

 avatar
unknown
plain_text
2 years ago
587 B
6
Indexable
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

double f4(int n);

int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    double sum = f4(n);
    printf("Sum of even elements: %.2lf\n", sum);
    return 0;
}

double f4(int n) {
    double num;
    if (n == 0) return 0; // base case
    scanf("%lf", &num);
    if (n % 2 == 0) { // if the position is even
        return num + f4(n - 1); // add the number and call recursively with n-1
    }
    else {
        return f4(n - 1); // call recursively with n-1
    }
}

Editor is loading...