I am trying to reverse the number that is doubled from the user input.
Problem is that if they enter a number like 20000 it gets doubled into 40000 but it reverses it to 2 rather than 4. How do i get it to make the doubled number reversed and the original number to not get reversed.
Here is my code, it is pretty simple but there is just that one problem...:
import java.util.Scanner;
import java.text.DecimalFormat;
public class ReverseNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
long input = s.nextLong();
long result = reverse(input);
DecimalFormat myFormatter = new DecimalFormat("#,##0");
System.out.println(myFormatter.format(input) + " doubled is " + myFormatter.format(input*2) + " and then reversed is " + myFormatter.format(reverse(result)));
}
public static long reverse(long n) {
long result = 0;
long rem;
while (n > 0) {
rem = n % 10;
n = n / 10;
result = result * 10 + rem;
}
return result;
}
}