All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Reverse A Number In Java

Last Updated : Mar 11, 2024

How To Reverse A Number In Java

In this article we will show you the solution of how to reverse a number in java, we will explore a simple and efficient approach to reverse a given number using mathematical operations and loops.

By the end of this tutorial, you will have a clear understanding of how to reverse any number in Java.

To reverse a number, we can follow the following steps

Initialize a variable to store the reversed number.Extract the digits from the original number one by one using the modulus operator.

Multiply the reversed number by 10 and add the extracted digit.Divide the original number by 10 to remove the extracted digit.

Repeat steps 2-4 until all digits of the original number have been processed.The final value of the reversed number will be stored in the variable initialized in step 1.

Step By Step Guide On How To Reverse A Number In Java :-

public class ReverseNumber {
    public static void main(String[] args) {
        int number = 12345;
        int reversedNumber = 0;
        while (number != 0) {
            int digit = number % 10;
            reversedNumber = reversedNumber * 10 + digit;
            number /= 10;
        }
        System.out.println("Reversed Number: " + reversedNumber);
    }
}
  1. In the main method, we declare and initialize the number variable with the original number we want to reverse, which is 12345. We also initialize the reversedNumber variable to 0, which will store the reversed number.
  2. We use a while loop to iterate until the number becomes 0. Inside the loop, we perform the following operations:
  3. The statement int digit = number % 10; extracts the last digit of the number by using the modulus operator % with a divisor of 10. For example, if number is 123, digit will be assigned the value 3.
  4. The statement reversedNumber = reversedNumber * 10 + digit; multiplies the reversedNumber by 10 to shift the existing digits to the left and then adds the extracted digit to the result. For example, if reversedNumber is 0 and digit is 3, after this statement, reversedNumber will become 3.
  5. The statement number /= 10; divides the number by 10 to remove the extracted digit. For example, if number is 123, after this statement, number will become 12.
  6. Once the loop completes, the reversed number will be stored in the reversedNumber variable. We then print the result using System.out.println().

Conclusion :-

In this tutorial, we learned how to reverse a number in Java. We used a simple mathematical approach and a while loop to extract the digits of the number one by one and build the reversed number.

Reversing a number is a common task in programming, and understanding this concept will help you solve similar problems efficiently in Java.

I hope this article on how to reverse a number in java helps you and the steps and method mentioned above are easy to follow and implement.