From 03b9a6eb9d4957b81bdd676b6f9b882c02a79adb Mon Sep 17 00:00:00 2001 From: Sejal6789 <113428962+Sejal6789@users.noreply.github.com> Date: Mon, 10 Oct 2022 10:55:42 +0530 Subject: [PATCH] Added Fibonacci using recursion in c++ --- Recursion/Fibonacci.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Recursion/Fibonacci.cpp diff --git a/Recursion/Fibonacci.cpp b/Recursion/Fibonacci.cpp new file mode 100644 index 00000000..e3671470 --- /dev/null +++ b/Recursion/Fibonacci.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +//recursive fibonacci function to find our nth digit of fibonacci series +int fibonacci(int x) { + if((x==1)||(x==0)) { + return(x);//if nth term is 0 or 1 it will return 0 or 1 + }else { + return(fibonacci(x-1)+fibonacci(x-2));//nth term is sum of its previous two terms + } +} +int main() { + int x; + cout << "Enter nth digit: "; + cin >> x; + cout << " " << fibonacci(x-1);//x-1 because 0 is our 1st term of fibonacci series + + return 0; +} \ No newline at end of file