#K12186. Sum of Even Fibonacci Numbers
Sum of Even Fibonacci Numbers
Sum of Even Fibonacci Numbers
Given a positive integer limit, your task is to calculate the sum of all even Fibonacci numbers that are less than or equal to this limit.
The Fibonacci sequence is defined as follows:
\(F_0 = 0,\ F_1 = 1,\ F_n = F_{n-1} + F_{n-2}\) for \(n \geq 2\).
For example, if the limit is 10
, the Fibonacci numbers not exceeding 10 are: 0, 1, 1, 2, 3, 5, 8. The even numbers among these are 2
and 8
, and their sum is 10
.
Note that if the provided limit is less than 2, the result should be 0
since no even Fibonacci numbers exist in that range.
inputFormat
The input consists of a single integer limit
provided via stdin. This number represents the upper bound; you must sum all even Fibonacci numbers that are \(\leq limit\).
outputFormat
Output a single integer via stdout, which is the sum of all even Fibonacci numbers less than or equal to the input limit
.
10
10