Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers. - C Function

C examples for Function:Utility Function

Description

Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.

Demo Code

#include <stdio.h>  

long Fibonacci(long n);

int main(void) {
  long n;/* w ww  . j  a va  2  s . com*/

  printf("Enter an integer n: ");

  while (scanf("%ld", &n) == 1){
    printf("Fibonacci #%ld = %ld\n", n, Fibonacci(n));

    printf("Enter an integer n: ");
  }

  return 0;
}
// return nth Fibonacci number
long Fibonacci(long n)
{
  if (n < 1){
    printf("Error: n must be a positive integer.\n");
    return -1;
  }

  long fib_n1 = 0, fib_n = 1, fib_n2;

  // n equals 1 or 2
  if (n == 1) 
     return 0;
  if (n == 2) 
     return 1;

  // n greater than or equal to 3
  for (long i = 3; i <= n; i++){
    // update old values
    fib_n2 = fib_n1;
    fib_n1 = fib_n;
    fib_n = fib_n1 + fib_n2;
  }

  return fib_n;
}

Related Tutorials