Python Exercise 11 Problem
In this Python exercise, write a Python program that will accept binary numbers as an input form a user. After the input, validate which binary numbers are divisible by 5 or not.
int() arguments
The Python int() method takes two arguments:
- x – Number or string to be converted to integer object. Default argument is zero.
- base – Base of the number in x. This can be 0 (code literal) or 2-36.
List of binary numbers:
1,10,11,100,110,111,1000,1001,1010,1011,1100,1110,1111,1000000,1000001,1000010,1000011,1000100,1000110,1000111,1001000,1001001,1001010,1001011,1001100,1001101,1001110,1001111,1010000,1010001,1010010,1010011,1010100,1010101,1010110,1010111,1011000,1011001,1011010,1011011,1011100,1011101,1011110,1011111,1100000,1100001,1100010,1100011,1100100
Solution Python Exercise 11
Python Code Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 | print("Please provide multiple sets of binary numbers that are comma separated: ") binary_numbers = input() numbers = [x for x in binary_numbers.split(',')] binary_divisible_by_5 = [] for b in numbers: binary_int = int(b, 2) if not binary_int % 5: binary_divisible_by_5.append(b) print("The list of numbers that are divisible by 5: ") print(','.join(binary_divisible_by_5)) |
User Input:
1 2 | Please provide multiple sets of binary numbers that are comma separated: 1,10,11,100,110,111,1000,1001,1010,1011,1100,1110,1111,1000000,1000001,1000010,1000011,1000100,1000110,1000111,1001000,1001001,1001010,1001011,1001100,1001101,1001110,1001111,1010000,1010001,1010010,1010011,1010100,1010101,1010110,1010111,1011000,1011001,1011010,1011011,1011100,1011101,1011110,1011111,1100000,1100001,1100010,1100011,1100100 |
Python Code Output:
1 2 | The list of numbers that are divisible by 5: 1010,1111,1000001,1000110,1001011,1010000,1010101,1011010,1011111,1100100 |