Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

18.8 — Print Characters Reversed I successfully wrote code for the scenario belo

ID: 3664692 • Letter: 1

Question

18.8 — Print Characters Reversed

I successfully wrote code for the scenario below. I would like someone from your team to rewrite the code using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE. My code can be found below. Thanks.

(Print the digits in an integer reversely) Write a recursive method that displays an int value reversely on the console using the following header:public static void reverseDisplay(int value)

For example, reverseDisplay(12345) displays 54321. Write a test program that prompts the user to enter an integer and displays its reversal.

This is my code for the program. It runs successfully, no issues. Please re-code using a different approach so I can compare and contrast a different ways to write the same program.

package Chapter_18;

import java.util.Scanner;

public class Exercise_08 {

    public static void main(String[] args) {

        System.out.print("Enter an integer: ");
        int value = new Scanner(System.in).nextInt();
        reverseDisplay(value);

    }

    public static void reverseDisplay(int value) {

        if (value <= 0)
            return;

        System.out.print(value % 10);
        reverseDisplay(value / 10);
    }
}

Explanation / Answer

package Chapter_18;

import java.util.Scanner;

public class Exercise_08 {

   public static void main(String[] args) {

       System.out.print("Enter an integer: ");
       int value = new Scanner(System.in).nextInt();
       reverseDisplay(value);

   }

   /**
   * Recursive function to reverse digits of num
   *
   * @param number
   */
   public static void reverseDisplay(int number) {
       if (number < 10) {
           System.out.print(number);
           return;
       } else {
           System.out.print(number % 10);
           reverseDisplay(number / 10);
       }
   }
}

OUTPUT:

Enter an integer: 12345
54321

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote