forked from purushottamnawale/geeksforgeeks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci Sum.cpp
More file actions
41 lines (39 loc) · 746 Bytes
/
Fibonacci Sum.cpp
File metadata and controls
41 lines (39 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
long long int fibSum(long long int N)
{
long long int sum = 1, t1 = 0, t2 = 1, nextTerm;
if (N == 0 || N == 1)
{
return N;
}
for (int i = 1; i < N; i++)
{
nextTerm = (t1 + t2) % 1000000007;
sum += nextTerm;
t1 = t2;
t2 = nextTerm;
}
return sum % 1000000007;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
long long int N;
cin >> N;
Solution ob;
cout << ob.fibSum(N) << endl;
}
return 0;
}
// } Driver Code Ends