fork download
  1. #include <stdio.h>
  2. int fibonacci(int n) {
  3. if(n==0){
  4. return 0;
  5. }
  6. if(n==1){
  7. return 1;
  8. }
  9. int x0=0;
  10. int x1=1;
  11. int xi;
  12. for(int i=2; i<=n; i++){
  13. xi=x0+x1;
  14. x0=x1;
  15. x1=xi;
  16. }
  17. return xi;
  18. }
  19.  
  20. int main() {
  21. int n;
  22. scanf("%d",&n);
  23. printf("%d",fibonacci(n));
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 5308KB
stdin
4
stdout
3