Menu
Coddy logo textTech

Even Fibonacci numbers

Lesson 5 of 20 in Coddy's Mathematical Riddles course.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed N, find the sum of the even-valued terms.

challenge icon

Challenge

Easy

Write python code calcEvenFibonacciNums that get a natural number, <i>N</i>, and return the sum of  the even terms of the Fibonacci sequence, starting with 1 and 2, whose values do not exceed <i>N</i>.

For example: calcEvenFibonacciNums(100) = 2+8+34 = 44.

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "solution.h"

int main() {
    int n;
    if (scanf("%d", &n) != 1) n = 0;
    int r = calcEvenFibonacciNums(n);
    printf("%d\n", r);
    return 0;
}

All lessons in Mathematical Riddles

3Fibonacci sequence

Fibonacci numbersEven Fibonacci numbers

9Binary numbers

Introduction