Exercise: Compute the Fibonacci Number

Write code to solve the problem.

Question

Write a function that computes the nthn^{th} Fibonacci number.

  • The function should be called fib and should take as input a single integer value in a variable n. It should return an integer value representing the nthn^{th} Fibonacci number.
  • The function should use a loop instead of recursion.

Sample input:

fib(4)

Sample output:

3

Exercise: Compute the Fibonacci Number

Write code to solve the problem.

Question

Write a function that computes the nthn^{th} Fibonacci number.

  • The function should be called fib and should take as input a single integer value in a variable n. It should return an integer value representing the nthn^{th} Fibonacci number.
  • The function should use a loop instead of recursion.

Sample input:

fib(4)

Sample output:

3
C
#include <stdio.h>
int fib(int n)
{
// Write your code here...
return 0;
}
int main(void)
{
int n = 10;
printf("Fibonacci number at position %d is %d\n", n, fib(n));
return 0;
}