{"id":196,"date":"2025-05-28T12:34:16","date_gmt":"2025-05-28T07:04:16","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=196"},"modified":"2026-04-04T14:38:48","modified_gmt":"2026-04-04T09:08:48","slug":"python-program-to-check-armstrong-number","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/","title":{"rendered":"Python Program to Check Armstrong Number | Explained"},"content":{"rendered":"\n<p>Hello curious minds! In this article we&#8217;ll explore the Python program to check Armstrong Number. Explained comprehensively with examples, code, edge cases, dry run, time and space complexity.<\/p>\n\n\n\n<p>Here is the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/armstrong-numbers2727\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] for your quick reference.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-f9f5f6ef-515b-410d-bfc7-b2ff114d1dfb\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#0-examples-of-python-program-to-check-armstrong-number\" style=\"\">Examples of Python Program to Check Armstrong Number:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#1-solution\" style=\"\">Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#2-problem-statement\" style=\"\">Problem Statement<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#3-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#4-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#6-\" style=\"\"><\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#7-potential-edge-cases\" style=\"\">Potential Edge Cases:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#8-handling-edge-cases\" style=\"\">Handling Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/#9-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-examples-of-python-program-to-check-armstrong-number\">Examples of Python Program to Check Armstrong Number:<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Example 1:\n\n  Input: N = 153\n  Output: True\n  Explanation: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153\n\nExample 2:\n\n  Input: N = 371\n  Output: True\n  Explanation: 3^3 + 5^3 + 1^3 = 27 + 343 + 1 = 371<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: N = <\/span><span style=\"color: #B5CEA8\">153<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">125<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">27<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">153<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: N = <\/span><span style=\"color: #B5CEA8\">371<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">^<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">27<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">343<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">371<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understand by watching video<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"DSA in Python Course - Armstrong Number Explained - Part 8 [Hindi] | Code &amp; Debug\" width=\"814\" height=\"611\" src=\"https:\/\/www.youtube.com\/embed\/rzRwIe8UwQQ?start=1&#038;feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-solution\">Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-problem-statement\">Problem Statement<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> We aim to determine whether a given integer &#8220;num&#8221; is an Armstrong number (also known as a narcissistic number). An Armstrong number is one that is equal to the sum of its own digits each raised to the power of the number of digits.<\/p>\n\n\n\n<p><strong>Purpose:<\/strong> The function &#8220;isArmstrong&#8221; checks if the integer &#8220;num&#8221; is an Armstrong number by calculating the sum of its digits each raised to the power of the number of digits and comparing it to the original number.<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> A positive integer &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Output:<\/strong> A boolean value True if &#8220;num&#8221; is an Armstrong number, and False otherwise.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read <strong><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-number-program-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Palindrome Number Program in Python<\/span><\/mark><\/a><\/strong> | Leetcode #9<\/em><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong> To determine if a number is an Armstrong number, we can break the number into its individual digits, raise each digit to the power of the total number of digits, and sum these values. If the sum equals the original number, then it is an Armstrong number.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Convert the number to a string to determine the number of digits<\/li>\n\n\n\n<li>Initialize a variable to store the sum of the digits each raised to the power of the number of digits<\/li>\n\n\n\n<li>Use a loop to extract each digit, raise it to the appropriate power, and add the result to the sum<\/li>\n\n\n\n<li>Compare the computed sum to the original number to determine if it is an Armstrong number<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def isArmstrong(num):\n    k = len(str(num))\n    arm_sum = 0\n    n = num\n    while n > 0:\n        ld = n % 10\n        arm_sum += ld**k\n        n = n \/\/ 10\n    return arm_sum == num<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">isArmstrong<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    k = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">(num))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    arm_sum = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    n = num<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> n &gt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        ld = n % <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arm_sum += ld**k<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = n \/\/ <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> arm_sum == num<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Number of Digits:<\/strong> The variable &#8220;k&#8221; is set to the length of the string representation of &#8220;num&#8221;, giving the number of digits in &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Initialize Sum:<\/strong> The variable &#8220;arm_sum&#8221; is initialized to 0 to accumulate the sum of the digits raised to the power of &#8220;k&#8221;.<\/li>\n\n\n\n<li><strong>Loop to Calculate Sum:<\/strong>\n<ol class=\"wp-block-list\">\n<li>While &#8220;n&#8221; is greater than 0, the last digit &#8220;ld&#8221; is extracted using n % 10<\/li>\n\n\n\n<li>This digit is raised to the power of &#8220;k&#8221; and added to &#8220;arm_sum&#8221;<\/li>\n\n\n\n<li>The last digit is removed from &#8220;n&#8221; using integer division by 10<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Return Comparison:<\/strong> The function returns True if &#8220;arm_sum&#8221; is equal to &#8220;num&#8221;, and False otherwise.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample image given below, this example demonstrates when a number is <strong>not an Armstrong<\/strong> number.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-1024x576.png\" alt=\"Dry Run of the Python Program to Check Armstrong Number - 1\" class=\"wp-image-197\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstring-1.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcMqnWLQ4AoSQ-JlF0Y3OAu9kvaoDOWdOr9iob1g4uQasxBLmYOksQDZVEiTuOEdVifE2gL_slC5G9APhLAjvhw6DTEHLUIbS1d6yp60aui0ID3TieSYf8yEkgLBUVBykg5NQWEDQ?key=RIx7QoNfk3-6wKcVli1MLQ\" style=\"\" alt=\"Dry Run of the Python Program to Check Armstrong Number - 2\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-potential-edge-cases\">Potential Edge Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single-Digit Numbers:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Any single-digit number should return True as any number is equal to itself raised to the power of one (e.g., num = 5).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Zero:<\/strong>\n<ul class=\"wp-block-list\">\n<li>num = 0 should return True as 0^1 = 0<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Large Numbers:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The function should handle large numbers correctly and check if they are Armstrong numbers.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-handling-edge-cases\">Handling Edge Cases<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The current code handles single-digit numbers and zero correctly.<\/li>\n\n\n\n<li>It also works for large numbers due to Python&#8217;s ability to handle large integers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<p><strong>Time Complexity<\/strong>: <strong>&#8220;O(log10N + 1)&#8221;<\/strong> where &#8220;N&#8221; is the input number. The time complexity is determined by the number of digits in the input integer &#8220;N&#8221;. In the worst case when &#8220;N&#8221; is a multiple of 10 the number of digits in N is &#8220;log10 N + 1&#8221;.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the while loop we divide N by 10 until it becomes 0 which takes &#8220;log10N&#8221; iterations.<\/li>\n\n\n\n<li>In each iteration of the while loop we perform constant time operations like modulus and division and pushing elements into the vector.<\/li>\n<\/ul>\n\n\n\n<p><strong>Space Complexity<\/strong>: <strong>&#8220;O(1)&#8221;<\/strong> as only a constant amount of additional memory for the reversed number regardless of size of the input number.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-points-to-remember-for-competitive-programming\">Some Questions for You<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1775293646046\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">What happens if the number ends with trailing zeros (e.g., 1200)?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Trailing zeros are naturally removed during reversal because integer operations ignore leading zeros in the result. For example, reversing 1200 gives 21, not 0021. This is expected behavior and does not require special handling.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293653780\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Can integer overflow occur while reversing the number?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Yes, in languages like C++ or Java where integers have fixed limits. While building the reversed number, multiplying by 10 can exceed the integer range. In such cases, you must check for overflow before updating the result.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293663494\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">How does the algorithm behave for negative numbers?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>The logic remains the same, but you typically store the sign separately. Reverse the absolute value and then reapply the sign at the end. For example, -123 becomes -321.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293683726\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Is there any faster approach than repeatedly dividing by 10?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>No, because any method must process each digit at least once. Whether you use arithmetic operations or convert to a string, the lower bound remains proportional to the number of digits, i.e., O(log\u2081\u2080N).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293689152\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Why don\u2019t we use extra data structures like arrays or stacks here?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Because the problem can be solved optimally using pure arithmetic. Using extra data structures would increase space complexity to O(N) without improving time complexity, making the solution less efficient.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<div class=\"wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Watch our python dsa course on youtube (200+ questions)<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello curious minds! In this article we&#8217;ll explore the Python program to check Armstrong Number. Explained comprehensively with examples, code, edge cases, dry run, time and space complexity. Here is the [Problem Link] for your quick reference. Examples of Python Program to Check Armstrong Number: Understand by watching video Solution Problem Statement Objective: We aim<\/p>\n","protected":false},"author":1,"featured_media":198,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[8,10],"class_list":{"0":"post-196","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-math"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/armstrong-number-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":5,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":1126,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions\/1126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/198"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}