Python Program for Check if all digits of a number divide it

Python Program for Check if all digits of a number divide it

The goal is to check if all digits of a number divide the number itself. Let's break this down into steps:

  1. Extract each digit from the number.
  2. For each digit, check if it is not zero and if the number is divisible by this digit.
  3. If all digits divide the number, return True, otherwise return False.

Let's write the Python program for this:

def check_divisibility(n): temp = n # temporary variable to store the original number while temp > 0: digit = temp % 10 # extract the last digit if digit == 0 or n % digit != 0: # check if digit is zero or if it doesn't divide n return False temp //= 10 # remove the last digit return True # Test the function num = 128 if check_divisibility(num): print(f"All digits of {num} divide it.") else: print(f"Not all digits of {num} divide it.") 

In this example, the number 128 is taken as input. 1, 2, and 8 are the digits of this number, and all of them divide 128. Thus, the program will print "All digits of 128 divide it."

Note: This approach works for numbers where the order of magnitude is reasonable (i.e., not extremely large numbers), since we're iterating through each digit of the number.


More Tags

viewanimator android-pageradapter probability layout default-constructor datastax virtual-keyboard conditional-statements pandas-styles jquery-ui-sortable

More Programming Guides

Other Guides

More Programming Examples