-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17. RecursionBasicLvl.java
More file actions
164 lines (143 loc) · 4.43 KB
/
17. RecursionBasicLvl.java
File metadata and controls
164 lines (143 loc) · 4.43 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Prerequisites:
- Iteration / Loops
- Functions
Recursion makes the code simpler, rather than the code made using Iteration.
Recursion is a programming technique where a function calls itself repeatedly until it reaches a base case that stops the recursion.
In Java, recursion can be used to solve problems that have a recursive structure, such as tree traversals, factorial calculations, and Fibonacci sequences.
- Recursion can lead to concise and elegant code, especially for problems with a recursive structure.
- Recursion can be easier to implement than iterative solutions, especially for problems with complex logic.
- Recursion allows you to break down a problem into smaller sub-problems, solving each recursively until the base case is reached.
Recursion can lead to a StackOverflowError when the method calls itself too many times, exceeding the maximum stack size.
*/
/* Qs. Print Numbers from 5 to 1. */
public class Recursion {
public static void printNum(int n) {
if(n == 0) { //base case
return;
}
System.out.println(n); //print
printNum(n-1); //recursion
}
public static void main(String[] args) {
int n = 5;
printNum(n);
}
}
/* Qs. Print Numbers from 1 to 5. */
public class Recursion {
public static void printNum(int n) {
if(n == 6) { //base case
return;
}
System.out.println(n); //print
printNum(n+1); //recursion
}
public static void main(String[] args) {
int n = 1;
printNum(n);
}
}
/* Qs. Print sum of first n natural numbers. */
import java.util.*;
public class Recursion {
public static void printSum(int n, int sum) {
if(n == 0) {
System.out.println(sum);
return;
}
sum += n;
printSum(n-1, sum);
}
public static void main(String[] args) {
printSum(10, 0);
}
}
/* Qs. Print Factorial of a number n. */
import java.util.*;
public class Recursion {
public static int factNum(int n) {
if(n == 1 || n == 0) {
return 1;
}
int fact_nm1 = factNum(n-1);
int fact_n = n * fact_nm1;
return fact_n;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
System.out.print("Factorial is: " + factNum(n));
}
}
/* Qs. Print the Fibonacci Sequence till nth term. */
import java.util.*;
public class Recursion {
public static void printFib(int a, int b, int n) {
if(n == 0) {
return;
}
int c = a + b;
System.out.println(c);
printFib(b, c, n-1);
}
public static void main(String[] args) {
int a = 0, b = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
System.out.println(a);
System.out.println(b);
printFib(a, b, n-2);
}
}
/* Qs. Print x^n (stack height = n). */
import java.util.*;
public class Recursion {
public static int calcPow(int x, int n) {
if(n == 0) { //base case 1
return 1;
}
if(x == 0) { //base case 2
return 0;
}
int xPownm1 = calcPow(x, n-1);
int xPow = x * xPownm1;
return xPow;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value, x = ");
int x = sc.nextInt();
System.out.print("Enter the power, n = ");
int n = sc.nextInt();
System.out.println("Answer will be: " + calcPow(x, n));
}
}
/* Qs. Print x^n (stack height = logn). */
import java.util.*;
public class Recursion {
public static int calcPow(int x, int n) {
if(n == 0) { //base case 1
return 1;
}
if(x == 0) { //base case 2
return 0;
}
// if n is even
if(n % 2 == 0) {
return calcPow(x, n/2) * calcPow(x, n/2);
} else { // if n is odd
return calcPow(x, n/2) * calcPow(x, n/2) * x;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value, x = ");
int x = sc.nextInt();
System.out.print("Enter the power, n = ");
int n = sc.nextInt();
System.out.println("Answer will be: " + calcPow(x, n));
}
}