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;
   }
}

Recommended Answers

All 4 Replies

Here is the problem:
On line no. 9 you are passing original number to get it reversed

Instead store the doubled number in some temporary variable and then pass it to the reverse function.
that is:

 long temp= input*2;
 long result = reverse(temp);

So your saying to do it like this:

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 temp= input*2;
       long result = reverse(temp);
       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;
   }
}

Doing it this way will make be like input being 20, doubled is 40 and reversed 40? Doesn't doing it this way make doubled show up in twice rather than making one of the doubles flip/reverse (i think i worded that awkwardly). Here is an error log that i got (I think i may have forgot to do something in the code):

First Difference Occurs at: byte 82, line 1
On Line 1 its column 82
Line 1 Wrong: Enter a positive integer: 52,010,023 doubled is 104,020,046 and then reversed is 104,020,046
Line 1 Right: Enter a positive integer: 52,010,023 doubled is 104,020,046 and then reversed is 640,020,401 
You have only a First Line Error

Because you are printing (reverse of (reverse of 40)).

Just print result in line no. 12,not reverse(result)

commented: THANKS! +10

Wow, thanks! I have been beating my head against the wall on this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.