Print Reverse number in java program

import java.util.Scanner;
class ReverseNumber {
  public static void main(String args[]) {
    int n, reverse = 0;

    System.out.println("Enter the number to reverse");
    Scanner in = new Scanner(System.in);
    n = in.nextInt();
    while (n != 0) {
      reverse = reverse * 10;
      reverse = reverse + n % 10;
      n = n / 10;
    }
    System.out.println("Reverse of entered number is " + reverse);
  }
}

Step-by-Step Explanation:

1.  Import the Scanner Class:

import java.util.Scanner;

The Scanner class is imported to read input from the user.

2. Class Declaration:

class ReverseNumber {

This declares a class named ReverseNumber that contains the main method, which will execute the logic.

3. Main Method Declaration:

public static void main(String args[]) {

The main method is the entry point for the Java program. It is where execution begins.

4. Declare Variables:

int n, reverse = 0;

  • n: This integer variable will hold the number entered by the user.
  • reverse: This variable will store the reversed number, initialized to 0.

5. Prompt for User Input:

System.out.println(“Enter the number to reverse”);

The program asks the user to input a number that he wants to reverse.

6. Create Scanner Object and Read Input:

Scanner in = new Scanner(System.in);

n = in.nextInt();

A scanner object named in it is made to read the input from the user.

A call to nextInt() method reads an integer from the user, which is stored in the variable n. 

7. Reversing the Number Using While Loop:

while (n != 0) 

{

reverse = reverse * 10;         // Shift the current reverse number to the left       

reverse = reverse + n % 10;    // Add the last digit of n to reverse

n = n / 10;                     // Remove the last digit from n

   }

The while loop continues as long as n is not zero. The loop performs the following steps on each iteration:

1. Shift the Current Reverse: 

reverse = reverse * 10: Shift the existing digits of reverse to the left by multiplying it by 10 (or add a new digit at the unit place).

2. Extract the Last Digit of n:

reverse = reverse + n % 10: Here, the last digit of n is appended to reverse. The expression n % 10 extracts the last digit of n.

3. Remove the Last Digit from n :

n = n / 10: This deletes the last digit of n by dividing it by 10. This truncates the decimal part, effectively shifting all digits to the right.

These steps repeat until all digits of n are processed.

8. Print the Reversed Number:

System.out.println(“Reverse of entered number is ” + reverse);

After the while loop terminates, the program outputs the reversed number stored in the reverse

What Occurs When the Program Is Executed?

  • It prompts the user to input a number.
  • It reverses the digits of the number through the while loop.
  • It extracts the rightmost side digit, concatenating them in the reverse variable till n is reduced to 0.

Sample Output:

#Sample 1:

Input:

Enter the number to reverse

12345

Implementation:

In the first iteration:

  • reverse = 0 * 10 + 12345 % 10 = 0 + 5 = 5
  • n = 12345 / 10 = 1234

In the second iteration:

  • reverse = 5 * 10 + 1234 % 10 = 50 + 4 = 54
  • n = 1234 / 10 = 123

In the third iteration:

  • reverse = 54 * 10 + 123 % 10 = 540 + 3 = 543
  • n = 123 / 10 = 12

In the fourth iteration:

  • reverse = 543 * 10 + 12 % 10 = 5430 + 2 = 5432 
  • n = 12 / 10 = 1

In the fifth iteration:

  • reverse = 5432 * 10 + 1 % 10 = 54320 + 1 = 54321
  • n = 1 / 10 = 0

After these operations, n is 0, and the program prints  “Reverse of entered number is 54321” 

Output:

Reverse of entered number is 54321

#Example 2: Input a Single Digit

Input:

Enter the number to reverse

7

Execution:

The same operations are executed, but the loop will be run only one time because the number has one digit. Therefore, it will print \”Reverse of entered number is 7\”.

Output:

Reverse of entered number is 7

Conclusion:

The program can reverse the number that the user inputted. The while loop is used for each digit that needs to be processed, shifted and add into the reverse variable. The output prints the reversed number.

Leave a Comment

Your email address will not be published. Required fields are marked *