{"id":14287,"date":"2021-10-01T10:30:02","date_gmt":"2021-10-01T05:00:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=14287"},"modified":"2021-11-22T18:33:29","modified_gmt":"2021-11-22T13:03:29","slug":"python-program-to-print-all-harshad-numbers-within-given-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-all-harshad-numbers-within-given-range\/","title":{"rendered":"Python Program to Print all Harshad Numbers within Given Range"},"content":{"rendered":"
In this article, we will learn how to print Harshad numbers within a specific range in Python. You will learn what a Harshad number is, how to check whether a given number is a Harshad number, and how to write\u00a0a Python code that outputs all the Harshad numbers within the user-specified range.<\/p>\n
Harshad Number:<\/strong><\/p>\n If the given number is divisible by the sum of its constituent digits, we can conclude that the given number is a Harshad number.<\/p>\n Given the lower limit range and upper limit range, the task is to print all the Harshad numbers in the given range.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print all the Harshad numbers in the given range.<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In this article, we will learn how to print Harshad numbers within a specific range in Python. You will learn what a Harshad number is, how to check whether a given number is a Harshad number, and how to write\u00a0a Python code that outputs all the Harshad numbers within the user-specified range. Harshad Number: If …<\/p>\nGiven upper limit range =11\r\nGiven lower limit range =178<\/pre>\n
The Harshad numbers in the given range 11 and 178 are:\r\n12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100 102 108 110 111 112 114 117 120 126\r\n 132 133 135 140 144 150 152 153 156 162 171<\/pre>\n
Given upper limit range =160\r\nGiven lower limit range =378<\/pre>\n
The Harshad numbers in the given range 160 and 378 are:\r\n162 171 180 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247 252 261\r\n 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 360 364 370 372 375 378<\/pre>\n
Program to Print all Harshad Numbers within Given Range in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function checkharshadnumb() which accepts the number as an argument and\r\n# returns true if it is Harshad number else returns False.\r\n\r\n\r\ndef checkharshadnumb(numb):\r\n # intiialize sum of digits to 0\r\n sum_of_digits = 0\r\n # copy the number in another variable(duplicate)\r\n dup_number = numb\r\n # Traverse the digits of number using for loop\r\n while dup_number > 0:\r\n sum_of_digits = sum_of_digits + dup_number % 10\r\n dup_number = dup_number \/\/ 10\r\n # If a number divides perfectly, it is a Harshad number return True\r\n # Else return False.\r\n if(numb % sum_of_digits == 0):\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# Give the lower limit range as static input and store it in a variable.\r\nlowlimrange = 11\r\n# Give the upper limit range as static input and store it in another variable.\r\nupplimrange = 178\r\nprint('The Harshad numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor m in range(lowlimrange, upplimrange+1):\r\n # Inside the for loop pass the iterator value to checkharshadnumb() function.\r\n if(checkharshadnumb(m)):\r\n # If it returns true then print the iterator value.\r\n print(m, end=' ')\r\n<\/pre>\n
The Harshad numbers in the given range 11 and 178 are:\r\n12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100 102 108 110 111 112 114 117 120 126\r\n 132 133 135 140 144 150 152 153 156 162 171<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function checkharshadnumb() which accepts the number as an argument and\r\n# returns true if it is Harshad number else returns False.\r\n\r\n\r\ndef checkharshadnumb(numb):\r\n # intiialize sum of digits to 0\r\n sum_of_digits = 0\r\n # copy the number in another variable(duplicate)\r\n dup_number = numb\r\n # Traverse the digits of number using for loop\r\n while dup_number > 0:\r\n sum_of_digits = sum_of_digits + dup_number % 10\r\n dup_number = dup_number \/\/ 10\r\n # If a number divides perfectly, it is a Harshad number return True\r\n # Else return False.\r\n if(numb % sum_of_digits == 0):\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# Give the lower limit range and upper limit range as\r\n# user input using map(),int(),split() functions.\r\n# Store them in two separate variables.\r\nlowlimrange, upplimrange = map(int, input(\r\n 'Enter lower limit range and upper limit range separate bt spaces = ').split())\r\nprint('The Harshad numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor m in range(lowlimrange, upplimrange+1):\r\n # Inside the for loop pass the iterator value to checkharshadnumb() function.\r\n if(checkharshadnumb(m)):\r\n # If it returns true then print the iterator value.\r\n print(m, end=' ')\r\n<\/pre>\n
Enter lower limit range and upper limit range separate bt spaces = 160 378\r\nThe Harshad numbers in the given range 160 and 378 are:\r\n162 171 180 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247 252 261\r\n 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 360 364 370 372 375 378<\/pre>\n
\n