-
Notifications
You must be signed in to change notification settings - Fork 9
/
RecursionExercises.java
254 lines (246 loc) · 7.38 KB
/
RecursionExercises.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.util.Scanner;
//*******************************************************
// RecursionExercises.java
// the class reprsents RecursionExercises
// Author: liron mizrahi
//*******************************************************
public class RecursionExercises
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int choise;
boolean fcontinue = true;
// do while
do
{
// menu
System.out.println("Please Choose one of the following loops exercises: ");
System.out.println("1 - For exercise 2");
System.out.println("2 - For exercise 3");
System.out.println("3 - For exercise 4");
System.out.println("4 - For exercise 8");
System.out.println("5 - For exercise 9");
System.out.println("6 - For exercise 20");
System.out.println("7 - For exercise 22");
System.out.println("0 - To Exit " + "\n");
System.out.println("Enter your choise ---> ");
// take input
choise = scan.nextInt();
// switch case
switch (choise)
{
case 1:
{
System.out.println("Enter Number: ");
int num = scan.nextInt();
System.out.println(countEvenDigits(num));
break;
}// end of case 1
case 2:
{
System.out.println("Enter Number: ");
int num1 = scan.nextInt();
System.out.println(allEvenDigits(num1));
break;
}// end of case 2
case 3:
{
System.out.println("Enter size of the array: ");
int size = scan.nextInt();
int[] arr = new int[size];
for(int i = 0; i < size; i++)
{
arr[i] = scan.nextInt();
}
System.out.println(countEven(arr, size));
break;
}// end of case 3
case 4:
{
System.out.println("Enter Number: ");
int num2 = scan.nextInt();
System.out.println(isAlternating(num2));
break;
}// end of case 4
case 5:
{
System.out.println("Enter Number: ");
int num3 = scan.nextInt();
System.out.println(hasAlternating(num3));
break;
}// end of case 5
case 6:
{
System.out.println("Enter Number: ");
int num4 = scan.nextInt();
System.out.println(calculateTerm(num4));
break;
}// end of case 6
case 7:
{
System.out.println("Enter Number: ");
int num5 = scan.nextInt();
drawRuler(num5);
break;
}// end of case 7
case 0:
{
fcontinue = false;
break;
}
default:
{
System.out.println("Invalid option");
break;
}
}
} while (fcontinue);
System.out.println("Goodbye!!");
}// end of method main
/**
* method is boolan recursive which returns the num of even digits in a given number
* @parm int (num)
* @return int
*/
public static int countEvenDigits(int num)
{
if (num == 0)
{
return 0;
}
int lastDigit = num % 10;
if(lastDigit % 2 == 0)
{
return 1 + countEvenDigits(num / 10);
}
else
{
return countEvenDigits(num / 10);
}
}// end of method countEvenDigits
/**
* method is boolean recursive which returns true if all the digits in the number is even else false
* @parm int num
* @return boolean
*/
public static boolean allEvenDigits(int num)
{
if( num == 0)
{
return true;
}
int lastDigit = num % 10;
if(lastDigit % 2 == 0)
{
return allEvenDigits(num / 10);
}
else
{
return false;
}
}// end of method allEvenDigits
/**
* method is boolean recursive which returns the num of even number in an array of numbers
* @parm int[] arr, int index
* @return int
*/
public static int countEven(int[] arr, int size)
{
if(size == 0)
{
return 0;
}
int count = 0;
if(arr[size - 1] % 2 == 0)
{
count = 1;
}
return count + countEven(arr, size - 1);
}// end of method countEven
/**
* methdo is boolean recursive which returns true if it is an alternation number
* altenation number is a number in which each pair of adjacent digits has a different parity
* @parm int num
* @return boolean
*/
public static boolean isAlternating(int num)
{
if (num < 10)
{
return true;
}
int lastDigit = num % 10;
int secondLastDigit = (num / 10) % 10;
if(lastDigit % 2 == secondLastDigit % 2)
{
return false;
}
else
{
return isAlternating(num / 10);
}
}// end of method isAlternating
/**
* method is boolean recursive which returns true if there is at least one pair of alternating digits and if the number is a single digit it will return false
* alternating number is a number in which each pair of adjacent digits has a differnet parity
* @parm int num
* @return boolean
*/
public static boolean hasAlternating(int num)
{
if(num < 10)
{
return false;
}
int lastDigit = num % 10;
int secondLastDigit = (num / 10) % 10;
if(lastDigit % 2 != secondLastDigit)
{
return true;
}
else
{
return hasAlternating(num / 10);
}
}// end of method hasAlternatin
/**
* method return the n Term in a sequence
* @parm int
* @return int
*/
public static int calculateTerm(int num)
{
if(num <= 3)
{
return num;
}
if(num % 2 == 0)
{
return calculateTerm(num -1) + calculateTerm(num - 2) + calculateTerm(num - 3);
}
else
{
return Math.abs(calculateTerm(num - 1) - calculateTerm(num - 3));
}
}// end of method calculateTerm
/**
* method is boolean recursive which print a a ruler according to the given num
* @parm int
* @return void
*/
public static void drawRuler(int num)
{
if(num == 0)
{
return;
}
drawRuler(num - 1);
for(int i = 0; i < num; i++)
{
System.out.print("-");
}
System.out.println();
drawRuler(num -1);
}// end of method drawRuler
}// end of class RecursionExercises