Given a number N, you have to find the sum of fibonacci numbers starting from 2 till N. i.e., if N = 2 , sum=5 (2 + 3). Let us consider one more example. If N=3, sum = 10 (2 + 3 + 5)
Method 1:
Set sum = 2 and starting from 1 loop until N, calculate the fibonacci series & add the numbers to sum.
Implementation:
#include <stdio.h> int main() { int t; scanf("%d",&t); while(t--) { int n,res=0,i,prev,curr,next; scanf("%d",&n); prev=1; curr=1; for(i=1;i<=n;i++) { next=prev+curr; res=res+next; prev=curr; curr=next; } printf("%d\n",(res % 1000000007) ); } return 0; }
Input
First line - T, the number of test cases.
Each of next T lines contains single integer N.
First line - T, the number of test cases.
Each of next T lines contains single integer N.
Output
Sum till Nth Fibonacci Number
Sample Input
2
2
3
Sample Output
5
10