There are multiple solutions and approaches on how to reverse the digits of a number. But as always i would only be showing the shortest possible way. On this example we would be reversing the digits of a number arrays and for loops.
package com.javatutorialhq.java.examples;
/*
* This example source code demonstrates how
* to reverse the digits of the number
*/
public class ReverseDigit {
public static void main(String[] args) {
Integer value = 19823267;
// convert the number to character array
char[] val = value.toString().toCharArray();
StringBuffer reverseDigit = new StringBuffer();
// iterate through the digits starting at the last
for (int i = val.length - 1; i >= 0; i--) {
reverseDigit.append(val[i]);
}
// printing the result
System.out.println("Original Number=" + value);
System.out.println("Reverse of the Number=" + reverseDigit);
}
}
Reverse Digit Example Sample Output :
Running above example source code will give the following result
Original Number=19823267 Reverse of the Number=76232891