// // Program: fibonacci // // Purpose: test recursive function to compute the Nth // Fibonacci number. The Nth Fibonacci number F(N) is // defined to be: // N, if N is 0 or 1. // F(N-1) + F(N-2), if N > 1. // // Input: // N (which number to display). // Output: // the Nth Fibonacci number. // #include #include // has EXIT_SUCCESS // Function to compute n-th Fibonacci number. // Pre: n >=0. // Post: returns n-th Fibonacci number. int fibonacci(int n) { if (n < 2) return n; else return fibonacci(n-1) + fibonacci(n-2); } // Main program. int main(void) { int n; cout << "Enter a non-negativer number: "; cin >> n; if (n < 0) cout << "Number is negative\n"; else cout << "For n = " << n << ", the n-th Fibonacci number is " << fibonacci(n) << endl; return EXIT_SUCCESS; }