Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this: </p> <pre><code>while( n != 0 ) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } </code></pre> <p>Logic is to get a single digit in each iteration starting from unit place, until all digits are encountered.</p> <p><code>n</code> is the input no. <code>reverse</code> is the variable where reverse of <code>n</code> is stored after <code>while</code> is finished.</p> <p><code>%</code> operator when used with 10, gives you the digit at unit place. <code>/</code> operator when used with 10, goves you all the digits except the digit at unit place.</p> <p>When <code>n = 7364</code> and <code>reverse = 0</code></p> <p>in 1st iteration, loop will look like:</p> <pre><code>while(7364 != 0) // true { reverse = 0 * 10; // reverse = 0 reverse = 0 + 7364%10 // reverse = 4 n = 7364/10 // n = 736 } </code></pre> <p>in 2nd iteration:</p> <pre><code>while(736 != 0) // true { reverse = 4 * 10; // reverse = 40 reverse = 40 + 736%10 // reverse = 46 n = 736/10 // n = 73 } </code></pre> <p>in 3rd iteration:</p> <pre><code>while(73 != 0) // true { reverse = 46 * 10; // reverse = 460 reverse = 460 + 73%10 // reverse = 463 n = 73/10 // n = 7 } </code></pre> <p>in 4th iteration:</p> <pre><code>while(7 != 0) // true { reverse = 463 * 10; // reverse = 4630 reverse = 4630 + 7%10 // reverse = 4637 n = 7/10 // n = 0 } </code></pre> <p>in 5th iteration:</p> <pre><code>while(0 != 0) // false and loop ends { ... } </code></pre> <p>and we have <code>reverse = 4637</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload