/* * compute elements of the Fibonacci sequence using a loop * (more efficient than recursive version, but also more complicated) */ #include long fib(long n) { if (n <= 1) { return 1; } long minus2 = 1; long minus1 = 1; long current; for (long index = 2; index <= n; ++index) { current = minus2 + minus1; minus2 = minus1; minus1 = current; } return current; } int main(void) { long n; printf("enter index of the element you want\n"); if (scanf("%ld", &n) == 1) { printf("element %ld is %ld\n", n, fib(n)); } else { printf("not number\n"); return 1; } return 0; }