How To Reverse A Number In Java: A Step-by-Step Guide

How To Reverse A Number In Java: A Step-by-Step Guide

reverse a number in java

How To Reverse A Number In Java: A Step-by-Step Guide

How To Reverse A Number In Java: A Step-by-Step Guide

How To Reverse A Number In Java: A Step-by-Step Guide

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe

How To Reverse A Number In Java: A Step-by-Step Guide

Reversing a number in Java refers to the process of changing the order of digits in a given number. 

For example, if the original number is 8579, reversing it would yield 9758. The objective is to obtain a mirror image of the original number. Here, the least significant digit becomes the first significant digit, and vice versa.

Reversing a number in Java can be useful in various programming scenarios, such as – 

  • Solving mathematical problems
  • Checking for palindromic numbers
  • Manipulating numerical data

In this blog you get a clear understanding of how to reverse a number in java. What are the simplest and best ways to reverse a number in Java? For this more exciting information read the blog at the end will ensure that you clearly understand the logic behind reversing a number in java.


Checkout our courses on the Java programing below

Core Java Training

Advanced Java Course

Java Full Stack Course


 

Here is The Simple Logic of Reverse a Number In Java, Follow the following steps:

Step1 : Initialize a variable called “reversedNumber” to store the reversed number.

Step2 : While the original number is greater than 0 , do the following:

  • Find the remainder of the original number by using the modulo (%) operator.
  • Multiply the “reversedNumber” variable by 10 and add the remainder to it.
  • Divide the original number by 10 to eliminate the least significant digit.
  • Repeat the following steps until the original number becomes 0.

Simple Formula is: 


Here are the best Five Ways To Reverse a Number In Java:

Reverse a number in Java Using – 

  1. While Loop
  2. Do while loop
  3. Recursion
  4. String
  5. Array

Let’s understand one by one method


1. Reverse a number in Java Using the while loop

Let’s Take an example:

  • We have an originalNumber variable set to 6392
  • The while loop continues until the originalNumber becomes 0. 
  • In each iteration, we extract the last digit (remainder) using the modulo operator(%) and add it to the reversedNumber variable after multiplying it by 10. 
  • The originalNumber is then divided by 10 to remove the least significant digit. 
  • Once the loop ends, the reversedNumber contains the reversed value of the original number. 
  • When you run this code, the output will be “Reversed Number: 2936” .

Also Read : Tokens In Java


2. Reverse a number in Java Using do-while Loop

Let’s Take an example:

  • We have the “originalNumber” variable set to 5823.
  • The “do while” loop is used to ensure that the loop body executes at least once.
  • Inside the loop, we extract the last digit (remainder) using the modulo operator (%) and add it to the “reversedNumber” variable after multiplying it by 10.
  • The “originalNumber” is then divided by 10 to remove the least significant digit.
  • The loop continues until the “originalNumber” becomes 0.
  • Once the loop ends, the “reversedNumber” variable contains the reversed value of the original number.
  • When you run this code, the output will be “Reversed Number: 3285”.

3. Reverse a number in Java Using Recursion

The logic behind reversing a number using recursion can be explained as follows:

Step 1: Base Case:

Check if the number is less than 10. If it is, return the number itself as there is no further recursion needed. This act as the termination condition for the recursive function.

Step 2: Recursive Case:

  1. Find the remainder of the number when divided by 10. This provides us with the final digit of the number.
  2. By dividing the number by 10, calculate the quotient. This removes the last digit from the number.
  3. Recursively call the “reverseNumber()” function with the quotient as the argument.
  4. Multiply the remainder by 10 raised to the power of the number of digits in the quotient.
  5. Add the reversed value of the quotient (obtained from the recursive call) to the multiplied remainder.
  6. Return the sum as the reversed number.

Step 3:

The recursion continues until the base case is reached (when the number becomes less than 10), at which point the reversed number is gradually constructed and returned.

Simple Formula is: 

  • “number” represents the original number that is being reversed.
  • “remainder” is the last digit of the number obtained by taking the modulus (%) of the number with 10.
  • “quotient” is the remaining part of the number obtained by dividing the number by 10.
  • “reverseNumber(quotient)” represents the recursive call to reverse the remaining digits.
  • “Math.pow()” method to calculate the power of 10, which is required to correctly position the reversed digits in the final result.

When you run this code, the output will be “Reversed Number: 8741.”


4. Reverse a number in Java Using String

Let’s Take an example:

  • We have the “originalNumber” variable set to 5823
  • We call the “reverseNumber()” method with the original number as an argument. 
  • Inside the method, we convert the number to a string using “String.valueOf(number)”. 
  • We use the “StringBuilder” class to reverse the string representation of the number. We do that by calling the “reverse()” method. 
  • Finally, we convert the reversed string back to an integer using “Integer.parseInt()” and return the reversed number. 
  • When you run this code, the output will be “Reversed Number: 3285”.

5. Reverse a number in Java Using Array

Let’s Take an example:

  • We start by converting the input number to a string using “Integer.toString(number)”. 
  • We create an integer array called “digits” with a length equal to the number of digits in the input number.
  • We use a loop to extract each digit from the number () and store it in an array. 
  • We convert each character to an integer using “Character.getNumericValue()”.
  • We use another loop to reverse the digits array by swapping the elements at the corresponding positions from the start and end of the array.
  • Lastly, we convert the reversed array back to a number by multiplying each digit by the appropriate power of 10 and adding it to the “reversedNumber” variable.
  • When you run this code, the output will be “Reversed Number: 54321”.

Also Read:  Features Of Java 


How do I reverse a decimal number (7893.679) without using any string functions in Java?

To reverse a decimal number without using any string functions in Java, you can follow a similar logic as reversing an integer. However, you’ll need to separate the whole number part and the decimal part, reverse them individually, and then combine them back.

Here’s an example with an explanation:

  • We start by separating the decimal number into its whole number part and decimal part. 
  • We cast the original number to an integer to get the whole number part. Then we subtract it from the original number to get the decimal part.
  • We reverse the whole number part using a similar logic as reversing an integer. We repeatedly extract the last digit of the whole number using the modulo operator (%), and then build the reversed whole number by multiplying by 10 and adding the extracted digit.
  • To reverse the decimal part, we multiply it by 10 and convert it to an integer in each iteration to extract the digit. 
  • We then build the reversed decimal part by multiplying by 10 and adding the extracted digit.
  • Lastly, we combine the reversed whole number and decimal parts. 
  • To do this, we divide the reversed decimal part by the appropriate power of 10 using “Math.pow()”.

The power is determined by the number of digits in the reversed decimal part, obtained using –

“Math.floor(Math.log10(reversedDecimalPart)) + 1”


Conclusion 

Reversing a number in Java is a simple process involving basic arithmetic and string manipulation techniques. We can quickly reverse the number by following the process. Convert the number into a string; reverse that string; and convert it back into an integer. With the knowledge gained from this blog, you can now confidently reverse numbers in Java and apply this skill to various programming tasks.

Aniket

Aniket

Leave a Reply

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

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?