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.
Examples:
Example1:
Input:
Given Number =144
Output:
The given number [ 144 ] all the digits of the given number divides the given number
Example2:
Input:
Given Number =369
Output:
The given number [ 369 ] all the digits of the given number do not divide the given number
- Python Program to Find Strong Numbers in a List
- Python Program to Generate Strong Numbers in an Interval
- Python Program to Print Binary Representation of a Number
Program to Check whether all Digits of a Number Divide it in Python
Below are the ways to check whether all the digits of the given number divide it using Python
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Create a function checkdivide() which takes the given number as an argument and returns true if all the digits of the given number divide it else it returns false.
- Pass the given number as an argument to checkdivide() function.
- Inside the checkdivide() function.
- Convert the given number into list of digits using list(),map(),int() and split() functions.
- Loop in this digits list using For loop.
- Inside the For loop check whether the given number is divisible by the iterator(digit).
- If it is false then return False.
- After the end of for loop return True.
- Check if the functions return true or false using the If conditional Statement.
- If it is true then print all the digits of the given number divides the given number.
- Else print all the digits of the given number do not divide the given number.
- The Exit of the Program.
Below is the implementation:
# Create a function checkdivide() which takes the given number # as an argument and returns true if all the digits of the given number # divide it else it returns false. def checkdivide(gvnnumb): # Inside the checkdivide() function. # Convert the given number into list of digits # using list(),map(),int() and split() functions. digitslstt = list(map(int, str(gvnnumb))) # Loop in this digits list using For loop. for digt in digitslstt: # Inside the For loop check whether the given number # is divisible by the iterator(digit). # If it is false then return False. if(gvnnumb % digt != 0): return False # After the end of for loop return True. return True # Give the number as static input and store it in a variable. numb = 144 # Pass the given number as an argument to checkdivide() function. # Check if the functions return true or false using the If conditional Statement. if(checkdivide(numb)): # If it is true then print all the digits of # the given number divides the given number. print('The given number [', numb, '] all the digits of the given number divides the given number') # Else print all the digits of the given number do not divide the given number. else: print('The given number [', numb, '] all the digits of the given number do not divide the given number')
Output:
The given number [ 144 ] all the digits of the given number divides the given number
Method #2: Using For Loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Create a function checkdivide() which takes the given number as an argument and returns true if all the digits of the given number divide it else it returns false.
- Pass the given number as an argument to checkdivide() function.
- Inside the checkdivide() function.
- Convert the given number into list of digits using list(),map(),int() and split() functions.
- Loop in this digits list using For loop.
- Inside the For loop check whether the given number is divisible by the iterator(digit).
- If it is false then return False.
- After the end of for loop return True.
- Check if the functions return true or false using the If conditional Statement.
- If it is true then print all the digits of the given number divides the given number.
- Else print all the digits of the given number do not divide the given number.
- The Exit of the Program.
Below is the implementation:
# Create a function checkdivide() which takes the given number # as an argument and returns true if all the digits of the given number # divide it else it returns false. def checkdivide(gvnnumb): # Inside the checkdivide() function. # Convert the given number into list of digits # using list(),map(),int() and split() functions. digitslstt = list(map(int, str(gvnnumb))) # Loop in this digits list using For loop. for digt in digitslstt: # Inside the For loop check whether the given number # is divisible by the iterator(digit). # If it is false then return False. if(gvnnumb % digt != 0): return False # After the end of for loop return True. return True # Give the number as user input using the int(input()) function # and store it in a variable. numb = int(input('Enter some random number = ')) # Pass the given number as an argument to checkdivide() function. # Check if the functions return true or false using the If conditional Statement. if(checkdivide(numb)): # If it is true then print all the digits of # the given number divides the given number. print('The given number [', numb, '] all the digits of the given number divides the given number') # Else print all the digits of the given number do not divide the given number. else: print('The given number [', numb, '] all the digits of the given number do not divide the given number')
Output:
Enter some random number = 369 The given number [ 369 ] all the digits of the given number do not divide the given number
The digit 6 does not divide the given number so it returns False.
Related Programs:
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to count the number of digits present in a number
- python program to find whether a number is a power of two
- python program to calculate the number of digits and letters in a string
- python program to calculate the average of a numbers digits of every number in given list
- python program to swap all odd and even bits of a number
- python program to find the factorial of a number