# Recursion/Recursive Methods Part 2

Tue Apr 20 2021 02:04:33 GMT+0000 (UTC)

Saved by @ahmedqgqgq #java

```public class MyName {
// Fibonacci
// 0 1 2 3 4 5 6 7 8 9 10...etc.
// 0 1 1 2 3 5 8 13 21 34 55...etc.
static int fib(int num) {
if (num == 0 || num == 1) {
return num;
} else {
return fib(num - 1) + fib(num - 2);
}
}
public static void main(String[] args) {
System.out.println("fib = " + fib(7));
}
}
public class MyName {
static int n1 = 0, n2 = 1, n3;
static void printfib(int n) {
if (n > 0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.println(" " + n3);
printfib(n - 1);
}
}
public static void main(String[] args) {
System.out.println(n1 + " " + n2);
int num = 5;
printfib(num);
}
}
public class MyName {
static int n1 = 0, n2 = 1, n3;
static void printfib(int n) {
if (n > 0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.println(" " + n3);
printfib(n - 1);
}
}
public static void main(String[] args) {
System.out.println(n1 + " " + n2);
int num = 5;
printfib(num);
}
}
public class MyName {
static int sum(int n1, int n2) {
if (n1 == n2) {
return n1;
} else {
return n1 + sum(n1 + 1, n2);
}
}
public static void main(String[] args) {
System.out.println("sum = " + sum(4, 6));
}
}
public class MyName {
static int sum(int n1, int n2) {
if (n1 == n2) {
return n1;
} else {
if (n1 < n2) {
return n1 + sum(n1 + 1, n2);
} else {
return n1 + sum(n1 - 1, n2);
}
}
}
public static void main(String[] args) {
System.out.println("sum = " + sum(6, 4));
}
}

```
content_copyCOPY