Write C++ program to give specific Fibonacci number
- User
Write C++ program to give specific Fibonacci number
Assistant
Here is a simple C++ program that takes an input from the user and prints out the corresponding Fibonacci number:
c++#include <iostream> int fibonacci(int n) { if (n <= 1) { return n; } else { return fibonacci(n-1) + fibonacci(n-2); } } int main() { int n; std::cout << "Enter a number: "; std::cin >> n; std::cout << "The corresponding Fibonacci number is " << fibonacci(n) << std::endl; return 0; }This program defines a recursive function
fibonaccithat takes an integernas input and returns thenth Fibonacci number. Themainfunction prompts the user for an input, calls thefibonaccifunction to get the corresponding Fibonacci number, and then prints it out.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.