"Write a C++ program to find the nth Fibonacci number using recursion.
- User
"Write a C++ program to find the nth Fibonacci number using recursion.
- Assistant
Here's a C++ program that finds the nth Fibonacci number using recursion:
cpp#include <iostream> // Recursive function to find the nth Fibonacci number int fibonacci(int n) { if (n <= 1) { return n; } return fibonacci(n - 1) + fibonacci(n - 2); } int main() { int n; std::cout << "Enter the value of n: "; std::cin >> n; if (n < 0) { std::cout << "Invalid input! n must be non-negative." << std::endl; } else { int result = fibonacci(n); std::cout << "The " << n << "th Fibonacci number is: " << result << std::endl; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.