public class PrintBackwardThenForward
1: //PrintBackwardThenForward.java
2: //Prints the digits of an integer backward, then forward.
4: public class PrintBackwardThenForward
5: {
6: public static void main(String[] args)
7: {
8: System.out.println
9: (
10: "\nThis program prints the digits "
11: + "of a positive integer backward, then forward.\n"
12: );
14: System.out.print("123 becomes ");
15: printBackwardThenForward(123);
16: System.out.println();
17: System.out.print("1010 becomes ");
18: printBackwardThenForward(1010);
19: System.out.println();
20: System.out.print("246810 becomes ");
21: printBackwardThenForward(246810);
22: System.out.println();
23: System.out.print("7 becomes ");
24: printBackwardThenForward(7);
25: System.out.println();
26: }
28: public static void printBackwardThenForward(int n)
29: /**
30: Print the digits of an integer backward, then forward.
31: @param n The integer whose digits are to be printed
32: backward, then forward.
33: <p>Pre:<p>n has been initialized with a positive integer.
34: <p>Post:<p>The digits of n have been printed backward and then forward.
35: */
36: {
37: if (n < 10)
38: {
39: System.out.print(n);
40: System.out.print(n);
41: }
42: else
43: {
44: System.out.print(n % 10);
45: printBackwardThenForward(n / 10);
46: System.out.print(n % 10);
47: }
48: }
49: }