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

An example that demonstrates the use of a do…while loop to reverse an integer nu

ID: 3826796 • Letter: A

Question

An example that demonstrates the use of a do…while loop to reverse an integer number. Modify the program so that the reversal process is called from a value returning method instead of included in the main program. Use a value returning method that accepts the integer as a parameter and returns the reversed number.

import java.util.Scanner;

public class ReverseNumber

{

   //-----------------------------------------------------------------

   // Reverses the digits of an integer mathematically.

   //-----------------------------------------------------------------

   public static void main(String[] args)

   {

     int number, lastDigit, reverse = 0;

Scanner scan = new Scanner(System.in);

System.out.print("Enter a positive integer: ");

     number = scan.nextInt();

//This part needs to go in its own method now ^_^

     do {

         lastDigit = number % 10;

         reverse = (reverse * 10) + lastDigit;

         number = number / 10;

     } while (number > 0);

//Call your new method here (or within the output statement below)

     System.out.println("That number reversed is " + reverse);

}

}

Explanation / Answer

This exercise is easy to implement. You just need to call the method at that place instead of do-while loop and the do-while loop should be written inside that function.

Source Code:

import java.util.Scanner;

public class ReverseNumber {
//-----------------------------------------------------------------
// Reverses the digits of an integer mathematically.
//-----------------------------------------------------------------

public static void main(String[] args) {
int number;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
number = scan.nextInt();

System.out.println("That number reversed is " + reverseNum(number));
}

public static int reverseNum(int number) {
int lastDigit, reverse = 0;
do {
lastDigit = number % 10;
reverse = (reverse * 10) + lastDigit;
number = number / 10;
} while (number > 0);

return reverse;
}
}

Description:

In the comments, it is mentioned that call that method here or in the output statement. I called it inside println() because you do not require to define a new variable and assign the value of reverseNum() to it.

reverseNum() is the required method asked in the question. It is taking 1 int parameter and returns its reverse (also int). Now, this method I declared static because it can be called from main() because, you can not call non-static method inside main() without using object of a class. But, here both the methods are in the same class. so defining it static helps here.

The number is passed from main(), processed the same way as given in the question and returned back which is printed on the output screen. It is displaying the reverse number when I executed the code.

Please comment if there is any query. Thank you. :)

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