#C13272. Sum of Fibonacci Numbers Up to n
Sum of Fibonacci Numbers Up to n
Sum of Fibonacci Numbers Up to n
Given an integer n, calculate the sum of all Fibonacci numbers that are less than or equal to n. The Fibonacci sequence is defined as follows:
\(F_0 = 0,\; F_1 = 1,\; F_n = F_{n-1} + F_{n-2}\) for \(n \ge 2\).
If n is zero or negative, the output should be 0.
For example, when n = 10, the Fibonacci numbers up to 10 are: 0, 1, 1, 2, 3, 5, 8, and the sum is 20.
Your program should read the input from standard input (stdin
) and output the result to standard output (stdout
).
inputFormat
The input consists of a single integer n provided via standard input.
Example:
10
outputFormat
Output a single integer which is the sum of all Fibonacci numbers that do not exceed n.
Example:
20## sample
0
0