{"id":15504,"date":"2021-04-30T18:31:24","date_gmt":"2021-04-30T18:31:24","guid":{"rendered":"https:\/\/www.askpython.com\/?p=15504"},"modified":"2021-05-10T07:01:05","modified_gmt":"2021-05-10T07:01:05","slug":"armstrong-number","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/armstrong-number","title":{"rendered":"Armstrong Number in Python &#8211; Easy Implementation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello there! Today let us learn something Interesting, Armstrong Number. We would be understanding what the number is and then implement a program to check if a number is an Armstrong Number or not.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Armstrong Number?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A number of <code>n<\/code> digits is an Armstrong number, if sum of each digit raised to the power of the no of digits is equal to the original number. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Armstrong Number definition<\/strong> is : abcd&#8230;(n-digits) = a^n + b^n + c^n + d^n + . . . . . and so on. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of Armstrong Number<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1 : 153<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Total number of digits = 3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Calculation (digit &#8211; wise ) = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The calculation done is directly equal to the original number. Hence the number is an Armstrong number. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2 : 548834 <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Total number of digits = 6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Calculation (digit &#8211; wise) = 5^6 + 4^6 +8^6 + 8^6 + 3^6 + 4^6 = 15625 + 4096 + 262144 + 262144 + 729 + 4096 = 548834<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The calculations done is directly equal to the original number. Hence the number is an Armstrong Number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm to check Armstrong Number<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To check if a number is an Armstrong number, one needs to follow the following steps<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Count the number of digits in the number.<\/li><li>Each digit is accessed one after another with the help of mod and division operations<\/li><li>Each digit is raised to the power of the number of digits and the result is stored in a separate variable<\/li><li>Steps 2 and 3 are repeated until the digits exhaust.<\/li><li>Check the result calculated with the original number<ul><li>If It matches: Armstrong Number<\/li><li>Otherwise: Not an Armstrong Number<\/li><\/ul><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PseudoCode for Armstrong Number<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The code below shows the pseudo code to check if a number is an Armstrong Number:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: true; highlight: [6,13]; title: ; notranslate\" title=\"\">\nREAD n\nCALCULATE NO OF DIGITS n_digit\nMAKE A COPY OF n\nresult=0\n\nCHECK DIGIT BY DIGIT:\n  WHILE n!=0\n     GET CURRENT DIGIT : digit = n % 10\n     UPDATE RESULT : result = result + digit^(n_digit)\n     TRIM THE LAST DIGIT : n = n \/ 10\n  ENDWHILE\n\nCHECK FOR ARMSTRONG NUMBER:\n   IF result==COPY OF n\n      PRINT &quot;ARMSTRONG NUMBER&quot;\n   ELSE\n      PRINT &quot;NOT AN ARMSTRONG NUMBER&quot;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Implementing Armstrong Number Checking in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we know about what Armstrong Number is and the steps to implement that, let&#8217;s implement the Armstrong checking line by line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create the intital variables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We first take an input <code>n<\/code> and then <a href=\"https:\/\/www.askpython.com\/python\/list\/length-of-a-list-in-python\" class=\"rank-math-link\">calculate the length<\/a> of the input. We also store a copy of the input so that no matter how much we change the original number, we have the copy to check Armstrong&#8217;s number later. We also initialized the result as 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code for the same is shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nn = input()\nn_digit = len(n)\nn=int(n)\ncopy_n=n\nresult = 0\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Traversing through the number and Updating Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To access each digit we take the modulus of the number ( mod 10 )  to extract the last digit of the number. The next step involves updating the result as the sum of the previous result and the digit raised to the power of the number of digits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The last and final step we take is divide the number by 10 to drop the last digit from the number. The same process is repeated until there are no more digits are left in the number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code for the same is shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nwhile(n!=0):\n    digit = n%10\n    result=result+pow(digit,n_digit)\n    n=int(n\/10)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Checking if the number is an Armstrong Number or not<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The final step is to check the copy of the number we created earlier with the result calculated to finally tell if the number is an Armstrong number or not. The code for the same is shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nif(result==copy_n):\n    print(&quot;Armstrong Number!&quot;)\nelse:\n    print(&quot;Not an Armstrong Number!&quot;)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Output Samples for the code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For now I tested the program for four inputs. The outputs for all four is shown below:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Number 1: 153<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n153\nArmstrong Number!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Number 2: 121<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n121\nNot an Armstrong Number!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Number 3: 548834 <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n548834\nArmstrong Number!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Number 4: 9468632<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n9468632\nNot an Armstrong Number!\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations! You have successfully learned about Armstrong Number and implemented the same!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But don&#8217;t stop here! Keep Reading and Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello there! Today let us learn something Interesting, Armstrong Number. We would be understanding what the number is and then implement a program to check if a number is an Armstrong Number or not. What is an Armstrong Number? A number of n digits is an Armstrong number, if sum of each digit raised to [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":15514,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-15504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/15504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=15504"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/15504\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/15514"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=15504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=15504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=15504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}