// exam 3 #4 solution: Fibonacci numbers // 0, 1, 1, 2, 3, 5, 8, 13, 21, ...... #include #include int Fibonacci(int n) // n is a nonnegative integer { int x, y, z; if (n == 0 ) y = 0; else { x = 0; y = 1; for (int j = 1; j < n; j++) { z = x + y; x = y; y = z; } } return y; } void main(void) { int i; for (i=0; i<=7; i++) { cout << "Fibonacci(" << i << ") = " << Fibonacci(i) << endl; } getchar(); }