title: Reversing an integer in Java using a for loop
body: <p><em>This is a homework problem</em> </p>
  
  <p>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.</p>
  
  <p>Here's the catch, you can't use String, StringBuffer, arrays, or other advanced structures in this problem.</p>
  
  <p>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 &lt; the length of the integer (number of digits)? How would I do that without String? </p>
  
  <p>Thanks for any input, and I'll add more information if requested. </p>
  
  <p>EDIT: </p>
  
  <p>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:</p>
  
  <pre><code>       int input = scan.nextInt();
          int n = input;
          int a = 0;
  
             for (int x = 0; n &gt; 0; x++){
                 n = n/10;
                  a = a + 1;
              }
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>This is what I have</p>
  
  <pre><code> int input = scan.nextInt();
          int n = input;
          int a = 0;
          int r = 0;
             for (int x = 0; n &gt; 0; x++){
                 n = n/10;
                  a = a + 1;
              }
             for (int y = 0; y &lt; n; y++) { 
                  r = r + input%10;
                  input = input/10;
              }
            System.out.println(input);
  </code></pre>
  
  <p>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? </p>