-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
35 lines (31 loc) · 847 Bytes
/
Copy pathRecursion.java
File metadata and controls
35 lines (31 loc) · 847 Bytes
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
// Recursion - technique of making a function call itself
// typically used to add a range of numbers together by breaking it dwn into sinmple tasks of adding 2 numbers
/*
When sum method is called ,it adds parameter k to the sum of all numbers smaller than k and returns the result when k is 0 the method returns 0
*/
public class Recursion {
static int myFunc(int k){
if (k < 11){
System.out.println(k);
return k + myFunc(k+1);
}
else{
System.out.println("compeleted");
return 0;
}
}
static int myFact(int y){
if(y > 1){
System.out.println(y);
return y * myFact(y-1);
}
else{
return 1;
}
}
public static void main(String[] args){
System.out.println(myFunc(5));
System.out.println("Factorial");
System.out.println(myFact(5));
}
}