From c1f26f43b55fa6658edbad50a29c598f8e063791 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gupta <154550063+vaishnavi-agr@users.noreply.github.com> Date: Tue, 29 Oct 2024 01:38:45 +0530 Subject: [PATCH] Fibonacci.cpp --- Fibonacci.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Fibonacci.cpp diff --git a/Fibonacci.cpp b/Fibonacci.cpp new file mode 100644 index 0000000..62df00a --- /dev/null +++ b/Fibonacci.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +// Function to calculate the nth Fibonacci number using recursion +int nthFibonacci(int n){ + + // Base case: if n is 0 or 1, return n + if (n <= 1){ + return n; + } + + // Recursive case: sum of the two preceding Fibonacci numbers + return nthFibonacci(n - 1) + nthFibonacci(n - 2); +} + +int main(){ + int n = 5; + int result = nthFibonacci(n); + cout << result << endl; + + return 0; +}