-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursionBasicss.java
130 lines (123 loc) · 3.4 KB
/
recursionBasicss.java
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class recursionBasicss{
//this function is for Decreasing number
public static void printDec(int n){
if(n==1){
System.out.print(n);
return;
}
System.out.print(n+" ");
printDec(n-1);
}
//This function is for Increasing number
public static void printInc(int n){
if(n==1){
System.out.print(n+" ");
return;
}
printInc(n-1);
System.out.print(n+" ");
}
//Factorial of a number n
public static int fact(int n){
if(n==0){
return 1;
}
int fnm1=fact(n-1);
int fn=n*fnm1;
return fn;
}
//print sum of first n natural numbers
public static int printSum(int n){
if(n==1){
return 1;
}
int fnm1=printSum(n-1);
int fn=n+fnm1;
return fn;
}
//Print Nth fibonacci number
public static int printFibo(int n){
if(n==0||n==1){
return n;
}
int fnm1=printFibo(n-1);
int fnm2=printFibo(n-2);
int fn=fnm1+fnm2;
return fn;
}
//Check if a array is sorted or not
public static boolean sortedArr(int arr[],int i){
if(i==arr.length-1){
return true;
}
if(arr[i]>arr[i+1]){
return false;
}
return sortedArr(arr,i+1);
}
//Function to find the first occurence of an array
public static int firstOccur(int arr[],int key,int i){
if(i==arr.length){
return -1;
}
if(arr[i]==key){
return i;
}
return firstOccur(arr,key,i+1);
}
//Function to find the last occurence of an element in an array
public static int lastOccur(int arr[],int key,int i){
if(i==arr.length){
return -1;
}
int isFound=lastOccur(arr,key,i+1);
if(isFound==-1 && arr[i]==key){
return i;
}
return isFound;
}
//Print x to the power n
public static int power(int a,int n){
if(n==0){
return 1;
}
int halfpower=power(a,n/2);
int halfpowersq=halfpower*halfpower;
if(n%2!=0){
halfpowersq=a*halfpowersq;
}
return halfpowersq;
}
//print all occurence of an element in an array
public static void allOccurence(int arr[],int key,int i){
if( i==arr.length){
return;
}
if(arr[i]==key){
System.out.print(i+" ");
}
allOccurence(arr, key, i+1);
}
//write a program to find length of a string
public static int lengthOstr(String str){
if(str.length()==0){
return 0;
}
return lengthOstr(str.substring(1))+1;
}
//you are given a number convert into string
static String digits[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
public static void printStr(int n){
if(n==0){
return;
}
int lastdigit=n%10;
printStr(n/10);
System.out.print(digits[lastdigit]+" ");
}
public static void main(String args[]){
int n=2024;
printStr(n);
System.out.println();
}
}