ID: 761

Write a C++ program to find the nth Fibonacci number using recursion.

Aligned LLM's Response:

Here is a C ++ program that finds the n th F ib on acci number using recursion : \n ``` \n # include <</a> iostream \n using namespace std ; \n \n int fib on acci ( int n ) { \n if ( n == 0 ) { \n return 0 ; \n } else if ( n == 1 ) { \n return 1 ; \n } else { \n return fib on acci ( n - 1 ) + fib on acci ( n - 2 ); \n } \n } \n \n int main () { \n int n ; \n cout << " Enter the value of n : "; \n cin n ; \n cout << " The " << n << " th F ib on acci number is : " << fib on acci ( n ) << endl ; \n return 0 ; \n } \n ``` \n This program defines a function ` f ib on acci ` that takes an integer ` n ` as input and returns the n th F ib on acci number . The function uses recursion to calculate the F ib on acci number by calling itself with ` n - 1 ` and ` n - 2 ` as arguments , and then adding the results together . \n \n In the ` main ` function , the program prompt s the user to enter the value of ` n `, and then calls the ` f ib on acci ` function with that value . The result is printed to the console . </s>

Legend: Shifted positions  |  Marginal positions  |  Unshifted positions

Token Distributions

Previous
Home
Next