
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if all digits of a number divide it in Python
Suppose we have a number n. We have to check whether all digits of it can divide n or not.
So, if the input is like n = 135, then the output will be True, because (135 / 1 = 135), (135 / 3 = 45) and (135 / 5 = 27).
To solve this, we will follow these steps −
- val := n
- while val > 0, do
- d := val mod 10
- if n is not divisible by d, then
- return False
- val := quotient of (val / 10)
- return True
Let us see the following implementation to get better understanding −
Example
def is_divisible(n, d) : return d != 0 and n % d == 0 def solve(n) : val = n while (val > 0) : d = val % 10 if not is_divisible(n, d): return False val = val // 10 return True n = 135 print(solve(n))
Input
135
Output
True
- Related Articles
- Python Program for Check if all digits of a number divide it
- C Program to Check if all digits of a number divide it
- PHP program to check if all digits of a number divide it
- Java Program to check if all digits of a number divide it
- Check if the frequency of all the digits in a number is same in Python
- Check if all bits of a number are set in Python
- C++ Program to check if a given number is Lucky (all digits are different)
- Number of digits that divide the complete number in JavaScript
- Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Find count of digits in a number that divide the number in C++
- Check if product of digits of a number at even and odd places is equal in Python
- Python - Check if a List contain particular digits
- Python - Check if list contain particular digits
- Check if the given decimal number has 0 and 1 digits only in Python
- Check if a number is magic (Recursive sum of digits is 1) in C++

Advertisements