Reversing an integer in Java using a for loop

10,466

Solution 1

While your original number is nonzero, take your result, multiply it by 10, and add the remainder from dividing the original by 10.

For example, say your original number is 12345. Start with a result of 0.

  1. Multiply result by 10 and add 5, giving you 5. (original is now 1234.)
  2. Multiply result by 10 and add 4, giving you 54. (original is now 123.)
  3. Multiply result by 10 and add 3, giving you 543. (original = 12.)
  4. Multiply result blah blah 5432. (original = 1.)
  5. Multiply, add, bam. 54321. And 1 / 10, in int math, is zero. We're done.

Your mission, should you choose to accept it, is to implement this in Java. :) (Hint: division and remainder are separate operations in Java. % is the remainder operator, and / is the division operator. Take the remainder separately, then divide the original by 10.)

Solution 2

You will need to use math to access each of the digits. Here's a few hints to get your started:

Use the % mod operator to extract the last digit of the number. Use the / division operator to remove the last digit of the number. Stop your loop when you have no more digits in the number.

Share:
10,466
user2130057
Author by

user2130057

Updated on June 04, 2022

Comments

  • user2130057
    user2130057 almost 2 years

    This is a homework problem

    How would I reverse an integer in Java with a for loop? The user will input the integer (I don't know how long it will be) and I need to reverse it. ie: If they enter 12345, my program returns 54321.

    Here's the catch, you can't use String, StringBuffer, arrays, or other advanced structures in this problem.

    I have a basic idea of what I need to do. My problem is...in the for loop, wouldn't the condition need to be x < the length of the integer (number of digits)? How would I do that without String?

    Thanks for any input, and I'll add more information if requested.

    EDIT:

    Of course, after introspection, I realized I should use another for loop to do this. What I did was create a for loop that will count the digits by dividing by 10:

           int input = scan.nextInt();
            int n = input;
            int a = 0;
    
               for (int x = 0; n > 0; x++){
                   n = n/10;
                    a = a + 1;
                }
    

    EDIT 2:

    This is what I have

     int input = scan.nextInt();
            int n = input;
            int a = 0;
            int r = 0;
               for (int x = 0; n > 0; x++){
                   n = n/10;
                    a = a + 1;
                }
               for (int y = 0; y < n; y++) { 
                    r = r + input%10;
                    input = input/10;
                }
              System.out.println(input);
    

    When I run it, it isn't reversing it, it's only giving me back the numbers. ie: if I put in 1234, it returns 1234. This doesn't make any sense to me, because I'm adding the last digit to of the input to r, so why wouldn't it be 4321?

    • eis
      eis about 11 years
      @zellio so do I, but homework is not forbidden here
    • Madbreaks
      Madbreaks about 11 years
      "What have you tried?"
    • flup
      flup about 11 years
      two answers: div and mod!
    • A4L
      A4L about 11 years
      you will not reverse an integer you'll revers just a string
    • Philippe A
      Philippe A about 11 years
      You can use (int)(Math.log10(yourInt)) + 1 to get the number of digits of your number.
    • nhahtdh
      nhahtdh about 11 years
      Use a while loop and % and /
    • eis
      eis about 11 years
      @user2130057 meta.stackexchange.com/questions/10811/… - so basically say that it is homework, and state what you have tried, and what problems you're having
    • zellio
      zellio about 11 years
      @eis - correct, it's not. But it should be tagged as such and accompanied by what the person has tried. We aren't an answer factory.
    • eis
      eis about 11 years
      @zellio it should not be tagged as such. See the explanation on homework tag wiki. The tag should not be used. However, the latter point is true.
    • user2130057
      user2130057 about 11 years
      Sorry guys, I wasn't trying to deceive anyone. I thought I had made it clear in the question what I had concluded (needed to use modulus). I'll do it correctly next time!
    • zellio
      zellio about 11 years
      @eis - huh, that is news to me, thanks. Didn't realize policy had changed, I've been off site for a while.