# Sum of numbers from 1 to N which are in Fibonacci Sequence

Given an integer N, the task is to find the sum of numbers from 1 to N which are in Fibonacci sequence.
First few Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, ….

Examples:

Input: N = 5
Output: 12
1 + 1 + 2 + 3 + 5 = 12

Input: N = 10
Output: 20
1 + 1 + 2 + 3 + 5 + 8 = 20

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Loop through all the Fibonacci numbers which are less than N.
• Initialize a sum variable with 0.
• Keep on adding these Fibonacci numbers to get the required sum.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the ` `// required sum ` `int` `fibonacciSum(``int` `N) ` `{ ` ` `  `    ``if` `(N == 0) ` `        ``return` `0; ` ` `  `    ``// Generate all Fibonacci numbers <= N ` `    ``// and calculate the sum ` `    ``int` `sum = 0; ` `    ``int` `a = 1, b = 1, c; ` `    ``sum += a; ` `    ``while` `(b <= N) { ` ` `  `        ``sum += b; ` `        ``int` `c = a + b; ` `        ``a = b; ` `        ``b = c; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 20; ` `    ``cout << fibonacciSum(N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `// Function to return the ` `// required sum ` `static` `int` `fibonacciSum(``int` `N) ` `{ ` ` `  `    ``if` `(N == ``0``) ` `        ``return` `0``; ` ` `  `    ``// Generate all Fibonacci numbers <= N ` `    ``// and calculate the sum ` `    ``int` `sum = ``0``; ` `    ``int` `a = ``1``, b = ``1``, c; ` `    ``sum += a; ` `    ``while` `(b <= N) ` `    ``{ ` ` `  `        ``sum += b; ` `        ``c = a + b; ` `        ``a = b; ` `        ``b = c; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``20``; ` `    ``System.out.println(fibonacciSum(N)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech. `

## Python3

 `# Python3 implementation of  ` `# the approach  ` ` `  `# Function to return the  ` `# required sum  ` `def` `fibonacciSum(N):  ` ` `  `    ``if` `N ``=``=` `0``: ` `        ``return` `0` ` `  `    ``# Generate all Fibonacci  ` `    ``# numbers <= N and calculate ` `    ``# the sum  ` `    ``Sum` `=` `0` `    ``a, b ``=` `1``, ``1` `    ``Sum` `+``=` `a  ` `    ``while` `b <``=` `N:  ` ` `  `        ``Sum` `+``=` `b  ` `        ``a, b ``=` `b, a ``+` `b  ` `     `  `    ``return` `Sum` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``N ``=` `20` `    ``print``(fibonacciSum(N))  ` ` `  `# This code is contributed  ` `# by Rituraj Jain `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the ` `// required sum ` `static` `int` `fibonacciSum(``int` `N) ` `{ ` ` `  `    ``if` `(N == 0) ` `        ``return` `0; ` ` `  `    ``// Generate all Fibonacci numbers <= N ` `    ``// and calculate the sum ` `    ``int` `sum = 0; ` `    ``int` `a = 1, b = 1, c; ` `    ``sum += a; ` `    ``while` `(b <= N) ` `    ``{ ` ` `  `        ``sum += b; ` `        ``c = a + b; ` `        ``a = b; ` `        ``b = c; ` `    ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `N = 20; ` `    ``Console.WriteLine(fibonacciSum(N)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech. `

## PHP

 ` `

Output:

```33
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : rituraj_jain, Code_Mech

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.