{"id":14925,"date":"2021-10-01T10:00:19","date_gmt":"2021-10-01T04:30:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=14925"},"modified":"2021-11-22T18:33:30","modified_gmt":"2021-11-22T13:03:30","slug":"python-program-to-check-whether-all-digits-of-a-number-divide-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-all-digits-of-a-number-divide-it\/","title":{"rendered":"Python Program to Check whether all Digits of a Number Divide it"},"content":{"rendered":"
Using a Python program, we will learn how to check whether all of the digits in a number divide it. We’ll divide the given number by each digit to see if it’s divisible. So you’ll learn how to retrieve a number’s individual digits, a method to check whether the number is divisible by its digits, and a Python program to do so.<\/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 check whether all the digits of the given number divide it using Python<\/p>\n Are you a job seeker and trying to find simple java programs for Interview?<\/a> This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.<\/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 The digit 6 does not divide the given number so it returns False. Using a Python program, we will learn how to check whether all of the digits in a number divide it. We’ll divide the given number by each digit to see if it’s divisible. So you’ll learn how to retrieve a number’s individual digits, a method to check whether the number is divisible by its digits, …<\/p>\nGiven Number =144<\/pre>\n
The given number [ 144 ] all the digits of the given number divides the given number<\/pre>\n
Given Number =369<\/pre>\n
The given number [ 369 ] all the digits of the given number do not divide the given number<\/pre>\n
Program to Check whether all Digits of a Number Divide it in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function checkdivide() which takes the given number\r\n# as an argument and returns true if all the digits of the given number\r\n# divide it else it returns false.\r\n\r\n\r\ndef checkdivide(gvnnumb):\r\n # Inside the checkdivide() function.\r\n # Convert the given number into list of digits\r\n # using list(),map(),int() and split() functions.\r\n digitslstt = list(map(int, str(gvnnumb)))\r\n # Loop in this digits list using For loop.\r\n for digt in digitslstt:\r\n # Inside the For loop check whether the given number\r\n # is divisible by the iterator(digit).\r\n # If it is false then return False.\r\n if(gvnnumb % digt != 0):\r\n return False\r\n # After the end of for loop return True.\r\n return True\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\nnumb = 144\r\n\r\n# Pass the given number as an argument to checkdivide() function.\r\n# Check if the functions return true or false using the If conditional Statement.\r\nif(checkdivide(numb)):\r\n # If it is true then print all the digits of\r\n # the given number divides the given number.\r\n print('The given number [', numb,\r\n '] all the digits of the given number divides the given number')\r\n# Else print all the digits of the given number do not divide the given number.\r\nelse:\r\n print('The given number [', numb,\r\n '] all the digits of the given number do not divide the given number')\r\n<\/pre>\n
The given number [ 144 ] all the digits of the given number divides the given number<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function checkdivide() which takes the given number\r\n# as an argument and returns true if all the digits of the given number\r\n# divide it else it returns false.\r\n\r\n\r\ndef checkdivide(gvnnumb):\r\n # Inside the checkdivide() function.\r\n # Convert the given number into list of digits\r\n # using list(),map(),int() and split() functions.\r\n digitslstt = list(map(int, str(gvnnumb)))\r\n # Loop in this digits list using For loop.\r\n for digt in digitslstt:\r\n # Inside the For loop check whether the given number\r\n # is divisible by the iterator(digit).\r\n # If it is false then return False.\r\n if(gvnnumb % digt != 0):\r\n return False\r\n # After the end of for loop return True.\r\n return True\r\n\r\n\r\n# Give the number as user input using the int(input()) function\r\n# and store it in a variable.\r\nnumb = int(input('Enter some random number = '))\r\n\r\n# Pass the given number as an argument to checkdivide() function.\r\n# Check if the functions return true or false using the If conditional Statement.\r\nif(checkdivide(numb)):\r\n # If it is true then print all the digits of\r\n # the given number divides the given number.\r\n print('The given number [', numb,\r\n '] all the digits of the given number divides the given number')\r\n# Else print all the digits of the given number do not divide the given number.\r\nelse:\r\n print('The given number [', numb,\r\n '] all the digits of the given number do not divide the given number')\r\n<\/pre>\n
Enter some random number = 369\r\nThe given number [ 369 ] all the digits of the given number do not divide the given number<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n