0% found this document useful (0 votes)
5 views5 pages

Java Recursion Examples and Exercises

Uploaded by

xaeabhishek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Java Recursion Examples and Exercises

Uploaded by

xaeabhishek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 import [Link].

*;
2 // WAP to print 1 to 5 numbers by using Recursion
3 class Main_REx1
4 {
5 public static void main(String[] arg)
6 {
7 Recursion1 r1 = new Recursion1();
8 [Link](1);
9 }
10 }
11 class Recursion1
12 {
13 void printNum(int n)
14 {
15 if (n==6)
16 return;
17 else
18 {
19 [Link](n);
20 printNum(n+1);
21 }
22 }
23 }
24 // Same Example For Better understanding of Recursion
25 //WAP to print 1 to 5 numbers by using Recursion
26 class Main_REx2
27 {
28 public static void main(String[] arg)
29 {
30 Recursion2 r2 = new Recursion2();
31 [Link](5);
32 [Link]("Hello");
33 }
34 }
35 class Recursion2
36 {
37 void printNum(int n)
38 {
39 if (n==0)
40 return;
41 else
42 {
43 printNum(n-1);
44 [Link](n);
45 }
46 }
47 }
48 //WAP to print 1 to n numbers in reverse order by using Recursion
49 class Main_REx3
50 {
51 public static void main(String[] arg)
52 {
53 Scanner sc = new Scanner([Link]);
54 [Link]("Enter n: ");
55 int n = [Link]();
56 Recursion3 r3 = new Recursion3();
57 [Link](n);
58 }
59 }
60 class Recursion3
61 {
62 void printNum(int n)
63 {
64 if (n==0)
65 return;
66 else
67 {
68 [Link](n);
69 printNum(n-1);
70 }
71 }
72 }
73 //WAP to do sum of print 1 to n numbers by using Recursion
74 class Main_REx4
75 {
76 public static void main(String[] arg)
77 {
78 Scanner sc = new Scanner([Link]);
79 [Link]("Enter n: ");
80 int n = [Link]();
81 Recursion4 r4 = new Recursion4();
82 [Link](n);
83 }
84 }
85 class Recursion4
86 {
87 int sum = 0;
88 void printNum(int n)
89 {
90 if (n==0)
91 {
92 [Link]("Sum of 1 to n = "+sum);
93 return;
94 }
95 else
96 {
97 sum = sum+n;
98 printNum(n-1);
99 }
100 }
101 }
102 //mathod 2 (with return type)
103 class Main_REx4_a
104 {
105 public static void main(String[] arg)
106 {
107 Scanner sc = new Scanner([Link]);
108 [Link]("Enter n: ");
109 int n = [Link]();
110 Recursion4_a r5 = new Recursion4_a();
111 [Link]("sum of 1to n = "+[Link](n));
112
113 }
114 }
115 class Recursion4_a
116 {
117 int add(int n)
118 {
119 if (n==0)
120 {
121 return 0;
122 }
123 else
124 {
125 return n + add(n-1);
126 }
127 }
128 }
129 //WAP to find factorial of a given numbers by using Recursion
130 class Main_REx5
131 {
132 public static void main(String[] arg)
133 {
134 Scanner sc = new Scanner([Link]);
135 [Link]("Enter n: ");
136 int n = [Link]();
137 Recursion5 r5 = new Recursion5();
138 [Link](n);
139 }
140 }
141 class Recursion5
142 {
143 int fact = 1;
144 void factorial(int n)
145 {
146 if (n==0)
147 {
148 [Link]("Factorial of n = "+fact);
149 return;
150 }
151 else
152 {
153 fact = fact*n;
154 factorial(n-1);
155 }
156 }
157 }
158 //mathod 2 (with return type)
159 class Main_REx5_a
160 {
161 public static void main(String[] arg)
162 {
163 Scanner sc = new Scanner([Link]);
164 [Link]("Enter n: ");
165 int n = [Link]();
166 Recursion5_a r5 = new Recursion5_a();
167 [Link]("Factorial of n = "+[Link](n));
168 }
169 }
170 class Recursion5_a
171 {
172 int factorial(int n)
173 {
174 if (n==1)
175 {
176 return 1;
177 }
178 else
179 {
180 return n*factorial(n-1);
181 }
182 }
183 }
184 //Practical:-54:- WAP to print Fibonacci Series upto given numbers of iterations by
using Recursion
185 class Main_REx6
186 {
187 public static void main(String[] arg)
188 {
189 Scanner sc = new Scanner([Link]);
190 [Link]("Enter n: ");
191 int n = [Link]();
192 Recursion6 r6 = new Recursion6();
193 [Link](n);
194 }
195 }
196 class Recursion6
197 {
198 int a = 0,b=1,c;
199 void printNum(int n)
200 {
201 if (n==0)
202 {
203 return;
204 }
205 else
206 {
207 c=a+b;
208 [Link](a+" ");
209 a=b;
210 b=c;
211 printNum(n-1);
212 }
213 }
214 }
215 /*Practical:-55:-WAP to find sum of digit of given number using recursion.*/
216 class Main_REx7
217 {
218 public static void main(String[] arg)
219 {
220 Scanner sc = new Scanner([Link]);
221 [Link]("Enter n: ");
222 int n = [Link]();
223 Recursion7 r7 = new Recursion7();
224 [Link](n);
225 }
226 }
227 class Recursion7
228 {
229 int sum=0,digit;
230 void printNum(int n)
231 {
232 if (n==0)
233 {
234 [Link]("Sum of digits of a given number = "+sum);
235 return;
236 }
237 else
238 {
239 digit = n%10;
240 sum = sum+digit;
241 n=n/10;
242 printNum(n);
243 }
244 }
245 }
246 /*Practical:-52:-WAP to find GCD of a given two numbers using recursion.*/
247 class Main_REx8
248 {
249 public static void main(String[] arg)
250 {
251 Scanner sc = new Scanner([Link]);
252 [Link]("Enter number 1: ");
253 int n1 = [Link]();
254 [Link]("Enter number 2: ");
255 int n2 = [Link]();
256 Recursion8 r8 = new Recursion8();
257 [Link](n1,n2);
258 }
259 }
260 class Recursion8
261 {
262 void printNum(int n1, int n2)
263 {
264 if (n1%n2==0)
265 {
266 [Link](n2);
267 return;
268 }
269 else
270 {
271 printNum(n2, n1%n2);
272 }
273 }
274 }
275 /*Practical:-52(Mathid 2):-WAP to find GCD of a given two numbers using recursion.*/
276 class Main_REx9
277 {
278 public static void main(String[] arg)
279 {
280 Recursion9 r9 = new Recursion9();
281 [Link]();
282 [Link](1);
283 }
284 }
285 class Recursion9
286 {
287 int n1,n2,GCD;
288 void getNum()
289 {
290 Scanner sc = new Scanner([Link]);
291 [Link]("Enter number 1: ");
292 n1 = [Link]();
293 [Link]("Enter number 2: ");
294 n2 = [Link]();
295 }
296 void printNum(int i)
297 {
298 if (i>n1)
299 {
300 [Link](GCD);
301 return;
302 }
303 else
304 {
305 if(n1%i==0 && n2%i==0)
306 {
307 GCD = i;
308 }
309 i++;
310 printNum(i);
311 }
312 }
313 }
314 //WAP to find factorial of a given numbers by using Recursion
315 class Main_REx10
316 {
317 public static void main(String[] arg)
318 {
319 Scanner sc = new Scanner([Link]);
320 [Link]("Enter n: ");
321 int n = [Link]();
322 [Link]("Enter r: ");
323 int r = [Link]();
324 Recursion10 r10 = new Recursion10();
325 int Fact_N = [Link](n);
326 [Link]("Factorial of n = "+Fact_N);
327 int Fact_R = [Link](r);
328 [Link]("Factorial of r = "+Fact_R);
329 int Fact_N_R = [Link](n-r);
330 [Link]("Factorial of n-r = "+Fact_N_R);
331
332 int nCr = (Fact_N)/(Fact_R*Fact_N_R);
333 [Link]("Result of nCr = "+nCr);
334 }
335 }
336 class Recursion10
337 {
338
339 int factorial(int n)
340 {
341 if (n==1)
342 {
343 return 1;
344 }
345 else
346 {
347 return n*factorial(n-1);
348 }
349 }
350 }
351

You might also like