Skip to content

Commit f458704

Browse files
author
jsquared21
committed
Add Ex 18.8
1 parent 1987f6c commit f458704

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: Exercise_18/Exercise_18_08/Exercise_18_08.class

773 Bytes
Binary file not shown.

Diff for: Exercise_18/Exercise_18_08/Exercise_18_08.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)