TechoSagar: Nth Fibonacci Number

Nth Fibonacci Number

Given a positive integer N, find the Nth fibonacci number. Since the answer can be very large, print the answer modulo 1000000007.

Input:

The first line of input contains T denoting the number of testcases.Then each of the T lines contains a single positive integer N.

Output:

Output the Nth fibonacci number.

Constraints:

1<=T<=200
1<=N<=1000

Example:

Input:
3
1
2
5
Output:
1
1
5
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int i;
int x=0,y=1;
long int number=1, result;
int num=1;
  while(num<=n)
{  //cout<<number<<" ";
  result=number;
number=(x+y)%1000000007;
x=y;
y=number;
num++;
}
cout<<result<<endl;
}
return 0;
}

Follow us

Follow Bijendra Kumar on Facebook