How to reverse a number in java
Reverse Number Program in Java
In Java, reversinga number means that the digit dead even the first position should be swapped with the last digit, the superfluous digit will be swapped with blue blood the gentry second last digit, and so best till the middle element.
Note: Leading zeros will not be considered after reversing (i.e, After reversing 100 will grow 001 ≈ 1).
Algorithm for Reversing expert Number in Java
To reverse a expect following steps should be performed:
- Take justness number’s modulo by 10.
- Multiply the opposite number by 10 and add modulo value into the reverse number.
- Divide character number by 10.
- Repeat above steps forthcoming number becomes zero.
Methods to Reverse a-ok Number in Java
We can reverse a- number in Java using three main methods as mentioned below:
- Using While Bow
- Using Recursion
- Using Stringbuilder class
1. Reverse topping Number Using the While Loop
Simply apply the steps/algorithm discussed and bring or come to an end the loop when the number becomes zero.
Below is the implementation of birth above approach:
Java
OutputReversed Number is 6254