Skip to content

Commit 7195234

Browse files
Add files via upload
added
1 parent cb3e8f5 commit 7195234

File tree

1 file changed

+379
-0
lines changed

1 file changed

+379
-0
lines changed

Recursion.java

Lines changed: 379 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
/*
2+
public class Recursion {
3+
public static void print_num(int n){
4+
if(n==0){
5+
return;
6+
}
7+
System.out.println(n);
8+
print_num(n-1);
9+
}
10+
public static void main(String[] args) {
11+
int n = 5;
12+
print_num(n); */
13+
14+
// 2
15+
16+
/*
17+
public static void print_sum(int i,int n,int sum){
18+
if(i == n){
19+
sum += i;
20+
System.out.println(sum);
21+
return;
22+
}
23+
sum += i;
24+
print_sum(i+1,n,sum);
25+
}
26+
public static void main(String[] args) {
27+
print_sum(1,10,0);
28+
}
29+
*/
30+
31+
// 3
32+
/* public static int factorial(int n){
33+
if( n==0 || n==1){
34+
return 1;
35+
}
36+
int fact_nm1 = factorial(n-1);
37+
int fact_n = n * fact_nm1;
38+
return fact_n;
39+
}
40+
41+
public static void main(String[] args) {
42+
int n = 5;
43+
System.out.println(factorial(n));
44+
}*/
45+
46+
// 4. fib
47+
48+
/* public static void fib_series(int a,int b,int n){
49+
if(n==0){
50+
return;
51+
}
52+
int c= a+b;
53+
System.out.println(c);
54+
fib_series(b,c,n-1);
55+
}
56+
57+
public static void main(String[] args) {
58+
int a=0,b=1;
59+
System.out.println(a);
60+
System.out.println(b);
61+
int n=7;
62+
fib_series(a,b,n-2);
63+
}
64+
}
65+
*/
66+
67+
// Tower_Of_Hanoi
68+
/*
69+
public class Recursion {
70+
public static void Tower_Of_Hanoi(int n,String src, String helper,String dest){
71+
72+
if(n == 1){
73+
System.out.println("Transfer Disk "+n+" From "+src+" To "+dest);
74+
return;
75+
}
76+
Tower_Of_Hanoi(n-1,src,dest,helper);
77+
System.out.println("Transfer Disk "+n+" From "+src+" To "+dest);
78+
Tower_Of_Hanoi(n-1,helper,src,dest);
79+
}
80+
81+
public static void main(String[] args) {
82+
int n =3;
83+
Tower_Of_Hanoi(n,"S","H","D");
84+
}
85+
}
86+
// OUTPUT:
87+
88+
Transfer Disk 1 From S To D
89+
Transfer Disk 2 From S To H
90+
Transfer Disk 1 From D To H
91+
Transfer Disk 3 From S To D
92+
Transfer Disk 1 From H To S
93+
Transfer Disk 2 From H To D
94+
Transfer Disk 1 From S To D
95+
*/
96+
97+
// 2.reverse string
98+
99+
/*
100+
public class Recursion {
101+
public static void string_rev(String str,int idx){ // TC=O(n)
102+
if(idx==0){
103+
System.out.print(str.charAt(idx));
104+
return;
105+
}
106+
System.out.print(str.charAt(idx));
107+
string_rev(str,idx-1);
108+
}
109+
110+
public static void main(String[] args) {
111+
String str = "Apple";
112+
string_rev(str,str.length()-1);
113+
}
114+
}*/
115+
116+
117+
118+
// Hi It A Recursion With Best Solution
119+
120+
// 1.Print Name 'N' Times using Recursion || TC=O(n) SC=O(n) ||
121+
122+
/*
123+
public class Recursion {
124+
public static void print_name(int i, int n){
125+
if(i>n){
126+
return;
127+
}
128+
System.out.println("NS Loni Good Morning");
129+
print_name(i+1,n);
130+
}
131+
132+
public static void main(String[] args) {
133+
int n =3;
134+
print_name(1,n);
135+
}
136+
}
137+
// output:
138+
139+
NS Loni Good Morning
140+
NS Loni Good Morning
141+
NS Loni Good Morning
142+
*/
143+
144+
145+
// 2. Print linearly From 1 to N
146+
/*
147+
public class Recursion {
148+
public static void print_name(int i, int n){
149+
if(i>n){
150+
return;
151+
}
152+
System.out.print(" "+i);
153+
print_name(i+1,n);
154+
}
155+
156+
public static void main(String[] args) {
157+
int n =5;
158+
print_name(1,n);
159+
}
160+
}
161+
//OUTPUT: 1 2 3 4 5
162+
*/
163+
164+
165+
166+
// 3. Print linearly From N to 1
167+
/*
168+
public class Recursion {
169+
public static void print_name(int i, int n){
170+
if(i<n){
171+
return;
172+
}
173+
System.out.print(" "+i);
174+
print_name(i-1,n);
175+
}
176+
177+
public static void main(String[] args) {
178+
int i = 5;
179+
int n = 1;
180+
print_name(i,n);
181+
}
182+
}
183+
// OUTPUT: 5 4 3 2 1
184+
*/
185+
186+
// 4. Print linearly From 1 to N WITH_OUT USING {i+1}
187+
/*
188+
public class Recursion {
189+
public static void print_name(int i, int n){
190+
if(i<1){
191+
return;
192+
}
193+
print_name(i-1,n);
194+
System.out.print(" "+i);
195+
}
196+
197+
public static void main(String[] args) {
198+
int n =5;
199+
print_name(n,n);
200+
}
201+
}*/
202+
// OUTPUT: 1 2 3 4 5
203+
204+
// 5. Print linearly From n TO 1--> WITH_OUT USING {i-1}
205+
/*
206+
public class Recursion {
207+
public static void print_name(int i, int n){
208+
if(i>n){
209+
return;
210+
}
211+
print_name(i+1,n);
212+
System.out.print(" "+i);
213+
}
214+
215+
public static void main(String[] args) {
216+
int n =5;
217+
int i=1;
218+
print_name(i,n);
219+
}
220+
}*/
221+
// OUTPUT: 5 4 3 2 1
222+
223+
224+
// 6.Sum n natural numbers
225+
/*
226+
public class Recursion {
227+
public static int sum_n_num(int n){
228+
if(n==0){
229+
return 0;
230+
}
231+
return n + sum_n_num(n-1);
232+
}
233+
234+
public static void main(String[] args) {
235+
int n = 5;
236+
int res = sum_n_num(n);
237+
System.out.println(res);
238+
}
239+
}
240+
// 15
241+
*/
242+
243+
// 7.Factorial TC = O(n)
244+
/*
245+
public class Recursion {
246+
public static int sum_n_num(int n){
247+
if(n==1){
248+
return 1;
249+
}
250+
return n * sum_n_num(n-1);
251+
}
252+
253+
public static void main(String[] args) {
254+
int n = 5;
255+
int res = sum_n_num(n);
256+
System.out.println("Factorial of Number: "+res);
257+
}
258+
}
259+
// output: Factorial of Number: 120
260+
*/
261+
262+
// 8.Fibonacci Series
263+
/*
264+
265+
public class Recursion {
266+
public static int fibo(int n){
267+
if(n==0){
268+
return 0;
269+
}
270+
if(n==1){
271+
return 1;
272+
}
273+
return fibo(n-1) + fibo(n-2);
274+
}
275+
276+
public static void print_fibo(int n){
277+
for (int i = 0; i < n; i++) {
278+
System.out.print(fibo(i)+" "); // 0 1 1 2 3
279+
}
280+
}
281+
282+
public static void main(String[] args) {
283+
int n = 5;
284+
int res = fibo(n);
285+
System.out.println("Fib of Numbers: "+res); // Fib of Numbers: 5
286+
print_fibo(n);
287+
}
288+
}
289+
*/
290+
291+
// 9. Reverse an Array
292+
/*
293+
public class Recursion {
294+
public static void swap(int arr[],int i, int j){
295+
int temp = arr[i];
296+
arr[i] = arr[j];
297+
arr[j] = temp;
298+
}
299+
public static void rev_array(int i, int arr[], int n){
300+
if(i>=n/2){
301+
return;
302+
}
303+
swap(arr ,i,n-i-1);
304+
rev_array(i+1, arr, n);
305+
}
306+
307+
public static void main(String[] args) {
308+
int arr[] = {1,2,3,4,5};
309+
int n = arr.length;
310+
rev_array(0,arr, n);
311+
for (int i = 0; i < n; i++) {
312+
System.out.print(arr[i]+" "); // 5 4 3 2 1
313+
}
314+
}
315+
}
316+
*/
317+
318+
// 10. Palindrome
319+
/*
320+
public class Recursion {
321+
public static boolean isPalindrome(String str) {
322+
return isPalindrome(str, 0, str.length() - 1);
323+
}
324+
325+
private static boolean isPalindrome(String str, int start, int end) {
326+
if (start >= end) {
327+
return true;
328+
}
329+
if (str.charAt(start) != str.charAt(end)) {
330+
return false;
331+
}
332+
return isPalindrome(str, start + 1, end - 1);
333+
}
334+
335+
public static void main(String[] args) {
336+
String palindrome1 = "madam";
337+
boolean isPalindrome1 = isPalindrome(palindrome1);
338+
System.out.println(palindrome1 + " is a palindrome: " + isPalindrome1);
339+
}
340+
}
341+
*/
342+
// output: madam is a palindrome: true
343+
344+
// 11.Print all SubSequence
345+
// Eg{1}: [3,1,2] ==> [],[1],[2],[3],[1,2],[1,3],[3,2],[3,1,2]---->[8]
346+
/*
347+
public class Recursion {
348+
public static void generateSubSequences(String str, String current, int index) {
349+
if (index == str.length()) {
350+
System.out.println(current);
351+
return;
352+
}
353+
generateSubSequences(str, current + str.charAt(index), index + 1);
354+
generateSubSequences(str, current, index + 1);
355+
}
356+
357+
public static void main(String[] args) {
358+
String str = "abc";
359+
generateSubSequences(str, "", 0);
360+
}
361+
}
362+
// OUTPUT:
363+
364+
abc
365+
ab
366+
ac
367+
a
368+
bc
369+
b
370+
c
371+
*/
372+
373+
374+
375+
376+
377+
378+
379+

0 commit comments

Comments
 (0)