Nth Fibonacci Number

PROBLEM :

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<=100
1<=N<=1000

Example:

Input:
3
1
2
5

Output:
0
1
3

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#define MOD 1000000007
long long Nth_Fibonacci_Number(int ) ;
int main()
 {
    int t,no ;
    cin>>t ;
    while(t--)
    {
        cin>>no ;
        cout<<Nth_Fibonacci_Number(no) ;
        cout<<endl ;
    }
return 0;
}

long long Nth_Fibonacci_Number(int no)
{
    if(no==1)
        return 0 ;
    if(no==2)
        return 1 ;
       
    long long a,b,c ;
    a=0 ;
    b=1 ;
   
    no=no-2 ;
    while(no)
    {
        c=a+b ;
        c=c%MOD ;
       
        a=b ;
        b=c ;
        no-- ;
    }
    return c ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )