Python Exercise: Print the 4 digit binary numbers that are divisible by 5
13. Filter 4-Digit Binary Numbers Divisible by 5
Write a Python program that accepts a sequence of comma separated 4 digit binary numbers as its input. The program will print the numbers that are divisible by 5 in a comma separated sequence.
Pictorial Presentation:

Sample Solution:
Python Code:
# Create an empty list named 'items'
items = []
# Take user input and split it into a list of strings using ',' as the delimiter
num = [x for x in input().split(',')]
# Iterate through each element 'p' in the 'num' list
for p in num:
# Convert the binary string 'p' to its decimal equivalent 'x'
x = int(p, 2)
# Check if 'x' is divisible by 5 (i.e., when divided by 5 there's no remainder)
if not x % 5:
# If 'x' is divisible by 5, add the binary string 'p' to the 'items' list
items.append(p)
# Join the elements in the 'items' list separated by ',' and print the result
print(','.join(items))
Sample Output:
0001,0010,0011,0100,0101,0110,0111 0101
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to accept a sequence of comma-separated 4-digit binary numbers and print those that are divisible by 5.
- Write a Python program to convert 4-digit binary strings to integers and filter out those divisible by 5.
- Write a Python program to use list comprehension to parse binary numbers and return a comma-separated string of those divisible by 5.
- Write a Python program to validate each 4-digit binary number and print only those whose decimal conversion is a multiple of 5.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program that accepts a sequence of lines (blank line to terminate) as input and prints the lines as output (all characters in lower case).
Next: Write a Python program that accepts a string and calculate the number of digits and letters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.