Interview Question

Intern Software Development Engineer Interview

Write an iterative Fibonacci function

Answer

Interview Answer

2 Answers

0

int a = 0;
int b = 1;
int numberOfFibs = 0;
fibonacci(int a, int b, int c) {
while ( count != numberOfFibs) {
  c = a + b;
  b = c;
  a = b;
  numberOfFibs++;
 }
}

Anonymous on Jan 12, 2014
0

int a = 0;
int b = 1;
int numberOfFibs = 0;
int count = 5; //for 0 1 1 2 3 5 8
void fibonacci(int a, int b, int c) {
while ( count > numberOfFibs) {
  c = a + b;
  b = c;
  a = b;
  numberOfFibs++;
 }
}

Anonymous on Jan 12, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.