-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion+function
97 lines (79 loc) · 1.56 KB
/
Recursion+function
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// FACTORIAL NUMBER
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a =sc.nextInt();
int ans = fact(a);
System.out.print(ans);
}
public static int fact(int n)
{
if(n==0){
return 1;
}
return n*fact(n-1);
}
}
// FIBONACCI SERIES
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a =sc.nextInt();
int ans = fib(a);
System.out.print(ans);
}
public static int fact(int n)
{
if(n==1){
return 0;
}
if(n==2){
return 1;
}
return fib(n-1)+fib(n-2);
}
}
// SUM OF DIGITS USING RECURSION
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a =sc.nextInt();
int ans = sum(a);
System.out.print(ans);
}
public static int sum(int n)
{
if(n==0){
return 0;
}
return n%10 + sum(n/10);
}
}
// REVERSE AN ARRAY
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] ={5, 4, 3, 2 ,1};
reverse(a , 0 , a.length -1);
System.out.print(Arrays.toString(a));
}
public static void reverse(int arr[], int start ,int end){
if(start>end){
return;
}
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
reverse(arr , start ,end);
}
}