{"id":6802,"date":"2023-11-04T17:17:50","date_gmt":"2023-11-04T11:47:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=6802"},"modified":"2023-11-10T12:17:21","modified_gmt":"2023-11-10T06:47:21","slug":"python-program-to-check-armstrong-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-armstrong-number\/","title":{"rendered":"Python Program to Check Armstrong Number"},"content":{"rendered":"
Armstrong Number:<\/strong><\/p>\n Beginners sometimes ask what the Armstrong number, also known as the narcissist number, is. Because of the way the number behaves in a given number base, it is particularly interesting to new programmers and those learning a new programming language. The Armstrong number meaning in numerical number theory is the number in any given number base that forms the sum of the same number when each of its digits is raised to the power of the number’s digits.<\/p>\n Ex: 153, 371 etc.<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Below are the ways to check Armstrong number in python<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Algorithm:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Armstrong Number: Beginners sometimes ask what the Armstrong number, also known as the narcissist number, is. Because of the way the number behaves in a given number base, it is particularly interesting to new programmers and those learning a new programming language. The Armstrong number meaning in numerical number theory is the number in any …<\/p>\nnumber =153<\/pre>\n
153 is Armstrong number<\/pre>\n
Here 1^3 + 5^3 + 3^3 = 153 so it is Armstrong Number<\/pre>\n
number =79<\/pre>\n
79 is not Armstrong number<\/pre>\n
Here 7^2 + 9^2 = 130 not equal to 79 so it is not Armstrong Number<\/pre>\n
Armstrong Number in Python<\/h2>\n
\n
Method #1: Using while loop<\/h3>\n
\n
\n
# given number\r\nnum = 153\r\n# intialize result to zero(ans)\r\nans = 0\r\n# calculating the total digits in the given number\r\ndigits = len(str(num))\r\n# copy the number in another variable(duplicate)\r\ndup_number = num\r\nwhile (dup_number != 0):\r\n\r\n # getting the last digit\r\n remainder = dup_number % 10\r\n\r\n # multiply the result by a digit raised to the power of the number of digits.\r\n ans = ans + remainder**digits\r\n dup_number = dup_number\/\/10\r\n# It is Armstrong number if it is equal to original number\r\nif(num == ans):\r\n print(num, \"is Armstrong number\")\r\nelse:\r\n print(num, \"is not Armstrong number\")\r\n<\/pre>\n
153 is Armstrong number<\/pre>\n
Method #2: By converting the number to string and Traversing the string to extract the digits<\/h3>\n
\n
# given number\r\nnum = 153\r\n# intialize result to zero(ans)\r\nans = 0\r\n# converting given number to string\r\nnumString = str(num)\r\n# calculating the number of digits of the given number\r\ndigits = len(numString)\r\n# Traverse through the string\r\nfor char in numString:\r\n # Converting the character of string to integer\r\n # multiply the ans by a digit raised to the power of digits.\r\n ans = ans+int(char)**digits\r\n\r\n# It is Armstrong number if it is equal to original number\r\nif(num == ans):\r\n print(num, \"is Armstrong number\")\r\nelse:\r\n print(num, \"is not Armstrong number\")\r\n<\/pre>\n
153 is Armstrong number<\/pre>\n
\n