Skip to content

Commit adfd498

Browse files
authoredJan 27, 2023
Update and rename RecursionExercisesAfekaCollege.java to RecursionExercises.java
1 parent da1b376 commit adfd498

File tree

2 files changed

+254
-253
lines changed

2 files changed

+254
-253
lines changed
 

‎RecursionExercises.java

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
import java.util.Scanner;
2+
//*******************************************************
3+
// RecursionExercises.java
4+
// the class reprsents RecursionExercises
5+
// Author: liron mizrahi
6+
//*******************************************************
7+
public class RecursionExercises
8+
{
9+
public static void main(String[] args)
10+
{
11+
Scanner scan = new Scanner(System.in);
12+
int choise;
13+
boolean fcontinue = true;
14+
15+
// do while
16+
do
17+
{
18+
// menu
19+
System.out.println("Please Choose one of the following loops exercises: ");
20+
System.out.println("1 - For exercise 2");
21+
System.out.println("2 - For exercise 3");
22+
System.out.println("3 - For exercise 4");
23+
System.out.println("4 - For exercise 8");
24+
System.out.println("5 - For exercise 9");
25+
System.out.println("6 - For exercise 20");
26+
System.out.println("7 - For exercise 22");
27+
System.out.println("0 - To Exit " + "\n");
28+
System.out.println("Enter your choise ---> ");
29+
// take input
30+
choise = scan.nextInt();
31+
// switch case
32+
switch (choise)
33+
{
34+
case 1:
35+
{
36+
System.out.println("Enter Number: ");
37+
int num = scan.nextInt();
38+
System.out.println(countEvenDigits(num));
39+
break;
40+
}// end of case 1
41+
case 2:
42+
{
43+
System.out.println("Enter Number: ");
44+
int num1 = scan.nextInt();
45+
System.out.println(allEvenDigits(num1));
46+
47+
break;
48+
}// end of case 2
49+
case 3:
50+
{
51+
System.out.println("Enter size of the array: ");
52+
int size = scan.nextInt();
53+
int[] arr = new int[size];
54+
for(int i = 0; i < size; i++)
55+
{
56+
arr[i] = scan.nextInt();
57+
}
58+
System.out.println(countEven(arr, size));
59+
break;
60+
}// end of case 3
61+
case 4:
62+
{
63+
System.out.println("Enter Number: ");
64+
int num2 = scan.nextInt();
65+
System.out.println(isAlternating(num2));
66+
break;
67+
}// end of case 4
68+
case 5:
69+
{
70+
System.out.println("Enter Number: ");
71+
int num3 = scan.nextInt();
72+
System.out.println(hasAlternating(num3));
73+
break;
74+
}// end of case 5
75+
case 6:
76+
{
77+
System.out.println("Enter Number: ");
78+
int num4 = scan.nextInt();
79+
System.out.println(calculateTerm(num4));
80+
break;
81+
}// end of case 6
82+
case 7:
83+
{
84+
System.out.println("Enter Number: ");
85+
int num5 = scan.nextInt();
86+
drawRuler(num5);
87+
break;
88+
}// end of case 7
89+
case 0:
90+
{
91+
fcontinue = false;
92+
break;
93+
}
94+
default:
95+
{
96+
System.out.println("Invalid option");
97+
break;
98+
}
99+
}
100+
} while (fcontinue);
101+
System.out.println("Goodbye!!");
102+
}// end of method main
103+
/**
104+
* method is boolan recursive which returns the num of even digits in a given number
105+
* @parm int (num)
106+
* @return int
107+
*/
108+
public static int countEvenDigits(int num)
109+
{
110+
if (num == 0)
111+
{
112+
return 0;
113+
}
114+
int lastDigit = num % 10;
115+
if(lastDigit % 2 == 0)
116+
{
117+
return 1 + countEvenDigits(num / 10);
118+
}
119+
else
120+
{
121+
return countEvenDigits(num / 10);
122+
}
123+
}// end of method countEvenDigits
124+
125+
/**
126+
* method is boolean recursive which returns true if all the digits in the number is even else false
127+
* @parm int num
128+
* @return boolean
129+
*/
130+
public static boolean allEvenDigits(int num)
131+
{
132+
if( num == 0)
133+
{
134+
return true;
135+
}
136+
int lastDigit = num % 10;
137+
if(lastDigit % 2 == 0)
138+
{
139+
return allEvenDigits(num / 10);
140+
}
141+
else
142+
{
143+
return false;
144+
}
145+
}// end of method allEvenDigits
146+
147+
/**
148+
* method is boolean recursive which returns the num of even number in an array of numbers
149+
* @parm int[] arr, int index
150+
* @return int
151+
*/
152+
public static int countEven(int[] arr, int size)
153+
{
154+
if(size == 0)
155+
{
156+
return 0;
157+
}
158+
int count = 0;
159+
if(arr[size - 1] % 2 == 0)
160+
{
161+
count = 1;
162+
}
163+
return count + countEven(arr, size - 1);
164+
}// end of method countEven
165+
166+
/**
167+
* methdo is boolean recursive which returns true if it is an alternation number
168+
* altenation number is a number in which each pair of adjacent digits has a different parity
169+
* @parm int num
170+
* @return boolean
171+
*/
172+
public static boolean isAlternating(int num)
173+
{
174+
if (num < 10)
175+
{
176+
return true;
177+
}
178+
int lastDigit = num % 10;
179+
int secondLastDigit = (num / 10) % 10;
180+
if(lastDigit % 2 == secondLastDigit % 2)
181+
{
182+
return false;
183+
}
184+
else
185+
{
186+
return isAlternating(num / 10);
187+
}
188+
}// end of method isAlternating
189+
190+
/**
191+
* 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
192+
* alternating number is a number in which each pair of adjacent digits has a differnet parity
193+
* @parm int num
194+
* @return boolean
195+
*/
196+
public static boolean hasAlternating(int num)
197+
{
198+
if(num < 10)
199+
{
200+
return false;
201+
}
202+
int lastDigit = num % 10;
203+
int secondLastDigit = (num / 10) % 10;
204+
if(lastDigit % 2 != secondLastDigit)
205+
{
206+
return true;
207+
}
208+
else
209+
{
210+
return hasAlternating(num / 10);
211+
}
212+
}// end of method hasAlternatin
213+
214+
/**
215+
* method return the n Term in a sequence
216+
* @parm int
217+
* @return int
218+
*/
219+
public static int calculateTerm(int num)
220+
{
221+
if(num <= 3)
222+
{
223+
return num;
224+
}
225+
if(num % 2 == 0)
226+
{
227+
return calculateTerm(num -1) + calculateTerm(num - 2) + calculateTerm(num - 3);
228+
}
229+
else
230+
{
231+
return Math.abs(calculateTerm(num - 1) - calculateTerm(num - 3));
232+
}
233+
}// end of method calculateTerm
234+
235+
/**
236+
* method is boolean recursive which print a a ruler according to the given num
237+
* @parm int
238+
* @return void
239+
*/
240+
public static void drawRuler(int num)
241+
{
242+
if(num == 0)
243+
{
244+
return;
245+
}
246+
drawRuler(num - 1);
247+
for(int i = 0; i < num; i++)
248+
{
249+
System.out.print("-");
250+
}
251+
System.out.println();
252+
drawRuler(num -1);
253+
}// end of method drawRuler
254+
}// end of class RecursionExercises

‎RecursionExercisesAfekaCollege.java

Lines changed: 0 additions & 253 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.