Python Program for Check if all digits of a number divide it
Last Updated : 13 Apr, 2023
Given a number n, find whether all digits of n divide it or not.
Examples:
Input : 128
Output : Yes
128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
Input : 130
Output : No
We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128 % d == 0 for d = 1, 2, 8. To do that, we need to iterate over each digit of the number.
python3 # Python 3 program to # check the number is # divisible by all # digits are not. # Function to check # the divisibility # of the number by # its digit. def checkDivisibility(n, digit) : # If the digit divides the # number then return true # else return false. return (digit != 0 and n % digit == 0) # Function to check if # all digits of n divide # it or not def allDigitsDivide( n) : temp = n while (temp > 0) : # Taking the digit of # the number into digit # var. digit = temp % 10 if ((checkDivisibility(n, digit)) == False) : return False temp = temp // 10 return True # Driver function n = 128 if (allDigitsDivide(n)) : print("Yes") else : print("No" ) # This code is contributed by Nikita Tiwari.
Time Complexity: O(log10n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach : Using str(),list(),map() and len() methods
Python3 # Python 3 program to check the number is # divisible by all digits are not. n = 128 x = str(n) a = list(map(int, x)) c = 0 for i in a: if(n % i == 0): c += 1 if(c == len(a)): print("Yes") else: print("No")
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Please refer complete article on Check if all digits of a number divide it for more details!
Approach#3: Using math
This approach is to factorize the number and check if each factor divides the number. If any factor of the number is not a digit or does not divide the number, then the answer is 'No'. Otherwise, the answer is 'Yes'.
Algorithm
1. Take the input number as n.
2. Factorize the number into a list of prime factors.
3. Traverse each factor of the number.
4. Check if the factor is a digit and divides the number.
5. If any factor is not a digit or does not divide the number, return 'No'.
6. Otherwise, return 'Yes'.
Python3 from math import sqrt def factorize(n): factors = [] while n % 2 == 0: factors.append(2) n = n // 2 for i in range(3, int(sqrt(n)) + 1, 2): while n % i == 0: factors.append(i) n = n // i if n > 2: factors.append(n) return factors def check_divide(n): factors = factorize(n) for factor in factors: if factor < 1 or factor > 9 or n % factor != 0: return 'No' return 'Yes' n=128 print(check_divide(n))
Time Complexity: O(sqrt(n) log n), where n is the input number.
Auxiliary Space: O(sqrt(n)), where n is the input number.
Similar Reads
Python Program to Check if a Number is Odd or Even Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if
2 min read
Python Program to Check If a Number is a Harshad Number Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
2 min read
Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read
Python Program to print all the numbers divisible by 3 and 5 for a given number Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
2 min read
Python Program to Find Numbers Divisible by Another Number We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples:Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the differ
3 min read