title: Reversing last n digits of an Integer in C
body: <p>I need to write a program that takes 2 digits(X and n) and then prints X with last n digits of X reversed.</p>
  <p>For example</p>
  <p>Input: 12345 3</p>
  <p>Output: 12543</p>
  <p>Input: 523 2</p>
  <p>Output: 532</p>
  <p>I already wrote a control mechanism for checking n is greater or equal than the number of digits of X
  For example if inputs are 6343 and 7, program prints that inputs should be changed and takes input again.</p>
  <p>My main problem is I couldn't find an algorithm for reversing last n digits. I can reverse any int with this code</p>
  <pre><code>  int X, r = 0;
  
    printf(&quot;Enter a number to reverse\n&quot;);
    scanf(&quot;%d&quot;, &amp;n);
  
    while (X != 0)
    {
      r = r * 10;
      r = r + n%10;
      X = X/10;
    }
  
    printf(&quot;Reverse of the number = %d&quot;, r);
  </code></pre>
  <p>But I couldn't figure how two reverse just last digits. Can you give me any idea for that?</p>