Write C++ program to give specific Fibonacci number
- User
Write C++ program to give specific Fibonacci number
Assistant
Here is a C++ program to give a specific Fibonacci number:
#include <iostream> using namespace std; int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } int main() { int n, fibNum; cout << "Enter which Fibonacci number you want: "; cin >> n; fibNum = fib(n); cout << "Fibonacci number " << n << " is " << fibNum << endl; return 0; }This program takes the n-th Fibonacci number as input from the user. It then calls a recursive fib() function to calculate the n-th Fibonacci number. This fib() function uses recursion and the fact that Fibonacci number is sum of previous two Fibonacci numbers if n > 1. Finally, it prints out the n-th Fibonacci number.
So it provides the desired functionality to output a specific Fibonacci number based on user input. The logic can be modified to store Fibonacci numbers in an array or vector as well for efficiency if needed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.