Java program to reverse the digits of a numbe.A. Name your program lab5.B. Start by asking the user to enter a number between 100 and 10,000.C. Check if the user has complied. D. Keep asking the user for a number between 100 and 10,000 until the user complies. E. Then print on the screen the digits of that number in reverse order.

Respuesta :

Answer:

//import the Scanner class to allow for user's inputs

import java.util.Scanner;

//Declaration of class lab5

public class lab5 {

   public static void main(String[] args) {

       //Create an object of the Scanner class to allow for user's input

       //Call the object input

       Scanner input = new Scanner(System.in);

       

       //Declare int variable to hold the user's number

       int number;

       

       //Create an infinite while loop to keep asking

       // the user to re-enter the number if the number is not within range

       while (true) {

           //Prompt the user to enter a number within the range.

           System.out.println("Please enter a number between 100 and 10,000");

           //Receive the number from the user and  

           //store in the integer variable called number

           number = input.nextInt();

           // Check if the number is within range.

          // If it is, then break out of the loop

          // Otherwise, repeat the loop

           if (number > 100 && number < 10000) {

               break;

           }

       }    // End of while loop

       //For simplicity, first convert the number to a string

       //by concatenating it with an empty space as follows

       String num = number + "";

       //Display a message describing the result

       System.out.println("The number in reverse is : ")

       //Now, print each character in the string in reverse order

       //using a for loop as follows

       for (int i = num.length() - 1; i >= 0; i--) {

           System.out.print(num.charAt(i));

       }

   }            // End of main method

}   // End of class declaration

===============================================================

Sample Output 1:

>> Please enter a number between 100 and 10,000

2

>> Please enter a number between 100 and 10,000

8000078

>> Please enter a number between 100 and 10,000

900

>> The number in reverse is :

009

=================================================================

=================================================================

Sample Output 2

>> Please enter a number between 100 and 10,000

256

>> The number in reverse is :

625

=================================================================

Explanation:

The code above contains comments explaining every part of the code. Please go through these comments.

For simplicity and clarification, the code is re-written as follows without comments;

===================================================

import java.util.Scanner;

public class lab5 {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       int number;

       while (true) {

           System.out.println("Please enter a number between 100 and 10,000");

           number = input.nextInt();

           if (number > 100 && number < 10000) {

               break;

           }

       }

       String num = number + "";

       System.out.println("The number in reverse is : ")

       for (int i = num.length() - 1; i >= 0; i--) {

           System.out.print(num.charAt(i));

       }

   }

}

============================================================

ACCESS MORE