n th fibonacci number-Iterative

#include <iostream>

using namespace std;

int main()
{
    int n,i,first,second,third;
    for(;;)
    {
        cin>>n;
        first=0;
        second=1;

        for(i=0;i<=n-2;i++)
        {
            third=first+second;
            first=second;
            second=third;
        }
        cout<<"The Fibonacci number for "<<n<<" is "<<third<<endl;
    }
    return 0;
}


Leave a comment