1
+ package com .udemy .dsapart1 .recursion ;
2
+ import java .util .Arrays ;
3
+ import java .util .Scanner ;
4
+
5
+ /**
6
+ * Section 2 : Recursion Programming problems for -
7
+ * - 1) Sum of Array elements using Recursion.
8
+ * - 2) Factorial of a number using Recursion.
9
+ * - 3) N-th number of Fibonacci Series using Recursion.
10
+ *
11
+ */
12
+ public class ArraySumFactorialFobonaciWithRecursion {
13
+
14
+ static double inputArr [];
15
+
16
+ public static void main (String [] args ) {
17
+ int sizeOfArr ;
18
+ Scanner scInpObj = new Scanner (System .in );
19
+ System .out .print ("\n Enter the size of Array : " );
20
+ sizeOfArr = scInpObj .nextInt ();
21
+ inputArr = new double [sizeOfArr ];
22
+ System .out .println ("\n Enter the Array elements : " );
23
+ pushElementsIntoArray (inputArr );
24
+ System .out .println ("\n Input Array : " + Arrays .toString (inputArr ));
25
+
26
+ System .out .println ("\n Sum of Array elements : " );
27
+ System .out .println ("*************************************************" );
28
+ int lastElementPosition =inputArr .length -1 ;
29
+ double sumOfItems =findSumOfArrayElements (lastElementPosition );
30
+ System .out .println (sumOfItems );
31
+
32
+ System .out .print ("\n Enter the number to compute factoral : " );
33
+ double inputNumToFact = scInpObj .nextDouble ();
34
+ System .out .println ("*************************************************" );
35
+ System .out .println ("\n Factorial of N : " );
36
+ double factOfNth =calculateFactorialOfN (inputNumToFact );
37
+ System .out .println (factOfNth );
38
+
39
+ System .out .print ("\n N-th number of Fibonacci Series using Recursion : " );
40
+ long nthNumInput = scInpObj .nextLong ();
41
+ System .out .println ("\n *************************************************" );
42
+ System .out .println ("" +nthNumInput +"th Fibonacci number : " );
43
+ long fiboNumVal =getNthFibonaciNumber (nthNumInput );
44
+ System .out .println (fiboNumVal );
45
+ }
46
+
47
+
48
+ /**
49
+ * N-th number of Fibonacci Series using Recursion.
50
+ * @param nthNumInput
51
+ *
52
+ */
53
+ private static long getNthFibonaciNumber (long nthNumInput ) {
54
+ if (nthNumInput == 0 || nthNumInput == 1 ) {
55
+ return nthNumInput ;
56
+ }
57
+ return getNthFibonaciNumber (nthNumInput - 1 ) + getNthFibonaciNumber (nthNumInput - 2 );
58
+ }
59
+
60
+
61
+ /**
62
+ * Factorial of a number using Recursion.
63
+ * @param inputNumToFact
64
+ *
65
+ */
66
+ private static double calculateFactorialOfN (double inputNumToFact ) {
67
+ if (inputNumToFact == 0 || inputNumToFact == 1 ) {
68
+ return 1 ;
69
+ }
70
+ return inputNumToFact * calculateFactorialOfN (inputNumToFact - 1 );
71
+ }
72
+
73
+
74
+ /**
75
+ * Sum of Array elements using Recursion.
76
+ * @param inputArr
77
+ *
78
+ */
79
+ private static double findSumOfArrayElements (int n ) {
80
+ if (n == 0 ) {
81
+ return inputArr [n ];
82
+ }
83
+ return inputArr [n ] + findSumOfArrayElements (n - 1 );
84
+ }
85
+
86
+
87
+ private static void pushElementsIntoArray (double inputArr []) {
88
+ Scanner scInpObj = new Scanner (System .in );
89
+ for (int i = 0 ; i < inputArr .length ; i ++) {
90
+ System .out .print ("\n Enter the element No " + (i + 1 ) + " : " );
91
+ inputArr [i ] = scInpObj .nextDouble ();
92
+ }
93
+ }
94
+
95
+ }
0 commit comments