Python Exercise: Print the 4 digit binary numbers that are divisible by 5
Python Conditional: Exercise-13 with Solution
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.
# 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))
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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join