// // example of recursion: // // function to compute n-th fibonacci number // // main (test) program repeatedly prompts for input // #include // precondition: n >= 0 // postcondition: returns n-th fibonacci number (where the sequence // starts with the 0-th number) int fibonacci(const int n); // ---- main program ---- int main() { int n; char prompt[] = "Enter a non-negative integer, or ctrl-D to end:\n"; cout << prompt; while (cin >> n) { if (n < 0) cout << "Number must be non-negative!\n"; else cout << "fibonacci(" << n << ") = " << fibonacci(n) << endl; cout << prompt; } return 0 ; } // ---- function definition ---- int fibonacci(const int n) { if (n < 2) return n; else return fibonacci(n-1) + fibonacci(n-2); }