-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.java
More file actions
54 lines (49 loc) · 1.69 KB
/
Copy pathrecursion.java
File metadata and controls
54 lines (49 loc) · 1.69 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
/*
* RECURSION :
* Recursion is when a function call itself recursively to perfrom a task which can be done using any loop control statement(For,While,Do-While,etc)
*
* Note : A very important point to consider while working with recursion is its base case(which terminates the recusrion)
* usually in loop conrtol statement we have terminating(loop stoping statement) which tell the complier when to stop the iteration
* but in case of recusrion we have to decide and delacre these statement very carefully or else the stackoverflow will occur.
*/
public class recursion {
static void printFun(int test){
if (test < 1)//base case
return;
else {
System.out.printf("%d ", test);
printFun(test - 1);
System.out.printf("%d ", test);
return;
}
}
static int Fib(int N){
if (N == 0 || N == 1)//base case
return N;
return Fib(N - 1) + Fib(N - 2);
}
static int Power(int n, int p){
if(p ==0){//base case
return 1;
}
return n * Power(n,p-1);
}
public static void main(String[] args) {
//printing a pattern using recursion
int test = 5;
while(test>=0){
printFun(test);
System.out.println("\n");
test--;
}
System.out.println("--------------------------------------\n");
//First ten fibonaci numbers
int range = 10;
for(int i=0;i<range;i++){
System.out.println(Fib(i));
}
System.out.println("--------------------------------------\n");
//power of a number
System.out.println(Power(2,5));
}
}