{"id":6717,"date":"2021-05-22T10:14:25","date_gmt":"2021-05-22T04:44:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=6717"},"modified":"2021-11-22T18:40:46","modified_gmt":"2021-11-22T13:10:46","slug":"disarium-number-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/disarium-number-in-python\/","title":{"rendered":"Disarium Number in Python"},"content":{"rendered":"
Disarium Number:<\/strong><\/p>\n A Disarium number is one in which the sum of each digit raised to the power of its respective position equals the original number.<\/p>\n like 135 , 89 etc.<\/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 Disarium number in python<\/p>\n Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Algorithm:<\/strong><\/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 Disarium Number: A Disarium number is one in which the sum of each digit raised to the power of its respective position equals the original number. like 135 , 89 etc. Example1: Input: number =135 Output: 135 is disarium number Explanation: Here 1^1 + 3^2 + 5^3 = 135 so it is disarium Number Example2: …<\/p>\nnumber =135<\/pre>\n
135 is disarium number<\/pre>\n
Here 1^1 + 3^2 + 5^3 = 135 so it is disarium Number<\/pre>\n
number =79<\/pre>\n
79 is not disarium number<\/pre>\n
Here 7^1 + 9^2 = 87 not equal to 79 so it is not disarium Number<\/pre>\n
Disarium Number in Python<\/h2>\n
\n
Method #1: Using while loop<\/h3>\n
\n
# given number\r\nnum = 135\r\n# intialize result to zero(ans)\r\nans = 0\r\n# calculating the digits\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 iterator value.\r\n ans = ans + remainder**digits\r\n digits = digits - 1\r\n dup_number = dup_number\/\/10\r\n# It is disarium number if it is equal to original number\r\nif(num == ans):\r\n print(num, \"is disarium number\")\r\nelse:\r\n print(num, \"is not disarium number\")\r\n<\/pre>\n
135 is disarium 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 = 135\r\n# intialize result to zero(ans)\r\nans = 0\r\n# make a temp count to 1\r\ncount = 1\r\n# converting given number to string\r\nnumString = str(num)\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 the iterator value.\r\n ans = ans+int(char)**count\r\n count = count+1\r\n# It is disarium number if it is equal to original number\r\nif(num == ans):\r\n print(num, \"is disarium number\")\r\nelse:\r\n print(num, \"is not disarium number\")\r\n<\/pre>\n
135 is disarium number<\/pre>\n
\n