Advertisement
volkovich_maksim

fibonacci (non-recursive)

Sep 10th, 2016
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. long int fib(int n)
  4. {
  5.    long int f1 = 0, f2 = 1;
  6.    int i;
  7.    for (i = 1; i <= n; i += 1)
  8.    {
  9.       f2 = f1+f2;
  10.       f1 = f2-f1;
  11.    }
  12.    return f1;
  13. }
  14.  
  15. int main(void)
  16. {  
  17.    int n;
  18.    int qualifier = 1;
  19.    while (qualifier == 1 || qualifier == 0)
  20.    {
  21.       qualifier = scanf("%d", &n);
  22.       if (qualifier == 1 && n >= 0)
  23.          {
  24.             printf("%ld\n", fib(n));
  25.          }
  26.       if (qualifier == 1 && n < 0)
  27.          {  
  28.             printf("wrong type\n");
  29.          }
  30.    }
  31.    return 0;
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement