1 //fibonacci,find the nth num. 1 1 2 3 5 8... 2 #include <iostream> 3 using namespace std; 4 5 int fib(int n){ 6 if(n==1 || n==2){ 7 return 1; 8 } 9 int prev=1; 10 int result=1; 11 n-=2; 12 while(n--){ 13 result+=prev; 14 prev=result-prev; 15 } 16 return result; 17 } 18 int main(){ 19 int n; 20 while(cin>>n){ 21 cout<<fib(n)<<endl; 22 } 23 24 return 0; 25 }