{"id":6709,"date":"2021-05-22T10:14:18","date_gmt":"2021-05-22T04:44:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=6709"},"modified":"2021-11-22T18:40:46","modified_gmt":"2021-11-22T13:10:46","slug":"harshad-number-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/harshad-number-in-python\/","title":{"rendered":"Harshad Number in Python"},"content":{"rendered":"
Harshad Number:<\/strong><\/p>\n A Harshad number is one whose original number is divisible by the sum of its digits.<\/p>\n like 5 , 18 , 156 etc.<\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Below are the ways to check harshad 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 Algorithm:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Harshad Number: A Harshad number is one whose original number is divisible by the sum of its digits. like 5 , 18 , 156 etc. Example 1: Input: number=18 Output: 18 is harshad number Explanation: Here sum_of_digits=9 i.e (1+8 ) and 18 is divisible by 9 Example 2: Input: number=19 Output: 19 is not harshad …<\/p>\nnumber=18<\/pre>\n
18 is harshad number<\/pre>\n
Here sum_of_digits=9 i.e (1+8 ) and 18 is divisible by 9<\/pre>\n
number=19<\/pre>\n
19 is not harshad number<\/pre>\n
Here sum_of_digits=10 i.e (1+ 9 ) and 19 is not divisible by 10<\/pre>\n
Harshad Number in Python<\/h3>\n
\n
Method #1: Using while loop<\/h3>\n
\n
# given number\r\nnum = 18\r\n# intiialize sum of digits to 0\r\nsum_of_digits = 0\r\n# copy the number in another variable(duplicate)\r\ndup_number = num\r\n# Traverse the digits of number using for loop\r\nwhile dup_number > 0:\r\n sum_of_digits = sum_of_digits + dup_number % 10\r\n dup_number = dup_number \/\/ 10\r\n# It is harshad number if sum of digits is equal to given number\r\n\r\nif(num % sum_of_digits == 0):\r\n print(num, \"is harshad number\")\r\nelse:\r\n print(num, \"is not harshad number\")\r\n<\/pre>\n
18 is harshad 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 = 18\r\n\r\n# Converting the given number to string\r\nnumString = str(num)\r\n\r\n# intiialize sum of digits to 0\r\nsum_of_digits = 0\r\n\r\n# Traverse through the string\r\nfor char in numString:\r\n # Converting the character of string to integer and adding to sum_of_digits\r\n sum_of_digits = sum_of_digits + int(char)\r\n\r\n\r\n# It is harshad number if sum of digits is equal to given number\r\n\r\nif(num % sum_of_digits == 0):\r\n print(num, \"is harshad number\")\r\nelse:\r\n print(num, \"is not harshad number\")\r\n<\/pre>\n
18 is harshad number<\/pre>\n
Method #3: Using list and map<\/h3>\n
\n
# given number\r\nnum = 18\r\n# Converting the given number to string\r\nnumString = str(num)\r\n# Convert the digits of given number to list using map function\r\nnumlist = list(map(int, numString))\r\n# calculate sum of list\r\nsum_of_digits = sum(numlist)\r\n\r\n# It is harshad number if sum of digits is equal to given number\r\nif(num % sum_of_digits == 0):\r\n print(num, \"is harshad number\")\r\nelse:\r\n print(num, \"is not harshad number\")\r\n<\/pre>\n
18 is harshad number<\/pre>\n
\n