File tree 2 files changed +35
-0
lines changed
Exercise_18/Exercise_18_08
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*********************************************************************************
2
+ * (Print the digits in an integer reversely) Write a recursive method that *
3
+ * displays an int value reversely on the console using the following header: *
4
+ * *
5
+ * public static void reverseDisplay(int value) *
6
+ * *
7
+ * For example, reverseDisplay(12345) displays 54321. Write a test program that *
8
+ * prompts the user to enter an integer and displays its reversal. *
9
+ *********************************************************************************/
10
+ import java .util .Scanner ;
11
+
12
+ public class Exercise_18_08 {
13
+ /** Main method */
14
+ public static void main (String [] args ) {
15
+ // Create a Scanner
16
+ Scanner input = new Scanner (System .in );
17
+
18
+ // Prompt the user to enter an integer
19
+ System .out .print ("Enter an integer: " );
20
+ int value = input .nextInt ();
21
+
22
+ // Display value reversely
23
+ reverseDisplay (value );
24
+ }
25
+
26
+ /** Method displays an int value reversely */
27
+ private static void reverseDisplay (int value ) {
28
+ if (value < 10 ) // Base case
29
+ System .out .println (value );
30
+ else {
31
+ System .out .print (value % 10 );
32
+ reverseDisplay (value / 10 ); // Recursive call
33
+ }
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments