-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
83 lines (76 loc) · 2.19 KB
/
Recursion.java
File metadata and controls
83 lines (76 loc) · 2.19 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
import javax.xml.transform.Source;
public class Recursion {
/*Recursion: recursion is a method of solving a computation problem where the solution depends on solution to smaller instances of the same problem.
* >>base case
* >> work
* >> inner function call f(n-1)
*/
public static void printDec(int n){
//base case
if (n == 1){
System.out.println(n);
return;
}
System.out.println(n + " ");
printDec(n-1);
}
public static void printincre(int m){
//base case
if(m== 1){
System.out.println(m+" ");
return;
}
printincre(m-1);
System.out.println(m+" ");
}
public static int factorial(int f){
if(f==0){
return 1;
}
int fnm1=factorial(f-1);
int fn = f*factorial(f-1);
return fn;
}
public static int sumNatural(int num1){
if(num1==1){
return 1;
}
int snm1= sumNatural(num1-1);
int Sn = num1+snm1;
return Sn;
}
public static int fib(int n) {
if(n==0 || n==1){
return n;
}
int fnm1=fib(n-1);
int fnm2 = fib(n-2);
int fibn=fnm1+fnm2;
return fibn;
}
// public static int sorted(int n){
// }
public static void main (String arg[]){
//printing number from n to 1
System.out.println("this is decreasing order");
int n = 3;
printDec(n);
// print number from n to 1 (increasing order)
System.out.println("this is increasing order");
int m=10;
printincre(m);
//print factorial of a number n
System.out.println("this is a factorial");
int f = 5;
System.out.println(factorial(f));
//print sum of first n natural numbers.
System.out.println("sum of natural number");
int num1 =5;
System.out.println(sumNatural(num1));
//fibonacci number
System.out.println("this is fibonacci number");
System.out.println(fib(n));
//print sorted array
System.out.println("sorted array");
}
}