Reverse Number in While Loop

How to Reverse a Number in Java: Step-by-Step Guide

Reversing a number is a common programming task that helps improve your understanding of loops and arithmetic operations. Whether you're preparing for a coding interview or just sharpening your Java skills, knowing how to reverse a number can be quite handy. In this blog, we'll dive into a simple Java program that reverses a number, breaking down the logic and providing a complete code example.

Why Learn to Reverse a Number?

Reversing a number can be useful in various scenarios, such as:

  • Palindromic Check: Determining if a number reads the same backward as forward.

  • Numerical Analysis: Simplifying certain mathematical computations.

  • Coding Practice: Enhancing your problem-solving and coding skills.

The Logic Behind Reversing a Number

Reversing a number involves extracting its digits one by one and constructing a new number in the reverse order. Here's a step-by-step breakdown of the logic:

  1. Initialize Variables: You'll need variables to store the original number, the reversed number, and a temporary variable to hold digits.

  2. Loop Until the Number Becomes Zero: Use a while loop to iterate as long as the number is not zero.

  3. Extract the Last Digit: Use the modulus operator % to get the last digit of the number.

  4. Build the Reversed Number: Multiply the reversed number by 10 and add the extracted digit.

  5. Remove the Last Digit: Use integer division / to remove the last digit from the original number.

  6. Repeat: Continue the process until all digits are reversed.

Java Program to Reverse a Number

Let's look at the complete Java code that implements this logic:

import java.util.Scanner;

public class shounak1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a, rev = 0, num;
        System.out.print("Enter the number: ");
        a = sc.nextInt();

        while (a != 0) {
            rev = (rev * 10) + a % 10;
            a = a / 10;
        }

        System.out.println("Reversed Number: " + rev);
    }
}

Breaking Down the Code

  • Importing Scanner: We use Scanner for user input.

  • Variable Initialization: a is the input number, rev stores the reversed number.

  • User Input: The program prompts the user to enter a number.

  • Reversing Logic: The while loop continues until the number a becomes zero. Inside the loop, the last digit is extracted and added to rev, then removed from a.

  • Output: Finally, the reversed number is printed.

Example

Let's see how this works with an example:

  • Input: 25

  • Output: 52

Step-by-Step Process:

  1. Initial number: 25

  2. Extract the last digit: 5 (25 % 10)

  3. Build reversed number: 0 * 10 + 5 = 5

  4. Remove the last digit: 25 / 10 = 2

  5. Extract the last digit: 2 (2 % 10)

  6. Build reversed number: 5 * 10 + 2 = 52

  7. Remove last digit: 2 / 10 = 0

  8. Loop ends, the reversed number is 52

Conclusion

Reversing a number in Java is a straightforward process that helps you understand basic programming concepts like loops and arithmetic operations. By following the steps outlined above and practicing with different numbers, you'll gain confidence in your coding skills.