-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh07_34_Recursion.java
More file actions
49 lines (44 loc) · 1.38 KB
/
Ch07_34_Recursion.java
File metadata and controls
49 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class Ch07_34_Recursion {
// Factorial of n = n * factorial(n-1)
static int factorial(int n){
if(n==0 || n ==1){
return 1;
}
return n * factorial(n-1);
}
static int fact(int x){
int fact1 = 1;
for (int f = x; f!=1;f--){
fact1 = fact1 * (f-1);
}
return fact1;
}
static int fibonacci(int t){
if (t<=0){
return 0;
}
else if (t == 1){
return 1;
}
else {
return fibonacci(t-1) + fibonacci(t-2);
}
}
public static void main(String[] args) {
System.out.println(" ");
// When a function calls itself without the need of external commands
// Factorial - 5! = 5x4x3x2x1;
//Can be done by For Loop (iterative) or with recursion
// Using recursion:-
System.out.print("Factorial of 5 is: ");System.out.println(factorial(5)); // Factorial of 5
// Using Iterative approach
System.out.println("Factorial of 5 using iterative approach: " + fact(6));
System.out.println(" ");
// Fibonacci Sequence using recursion
System.out.println("Fibonacci Series till 5 is: ");
for (int i = 0 ; i<=5; i++){
System.out.print(fibonacci(i) + ", ");
// Expected answer = 0,1,1,2,3,5
}
}
}