From e95c736fa11596add8f1254438cd2de771d0221f Mon Sep 17 00:00:00 2001 From: Ajinkya Narendra Mahajan <71584227+ajinkyamahajan02@users.noreply.github.com> Date: Mon, 17 Oct 2022 19:26:51 +0530 Subject: [PATCH] Create fibonacci series using recursion --- Programs/fibonacci series using recursion | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Programs/fibonacci series using recursion diff --git a/Programs/fibonacci series using recursion b/Programs/fibonacci series using recursion new file mode 100644 index 0000000..d1ec558 --- /dev/null +++ b/Programs/fibonacci series using recursion @@ -0,0 +1,34 @@ +public class FibonacciRecursion { + public static void main(String[] args) { + int n = 7; + + System.out.print("Fibonacci: "); + System.out.print("0\t"); + System.out.print("1\t"); + printFib(0,1,n-2); + + System.out.println("\nSum :"+ sumprintFib(n)); + } + + public static void printFib(int a, int b, int n) { + + if(n==0) + return; + + int c = a+b; + System.out.print(c + "\t"); + printFib(b, c, n-1); + } + + + public static int sumprintFib(int n) { + + if(n == 0) + return 0; + else if(n==1) + return 1; + + int sum = sumprintFib(n-1) + sumprintFib(n-2); + return sum; + } +}