Python: Count number of zeros and ones in the binary representation of a given integer
Python Basic - 1: Exercise-131 with Solution
Write a Python program to count the number of zeros and ones in the binary representation of a given integer.
Sample Solution
Python Code:
# Define a function 'test' that counts the number of ones and zeros in the binary representation of a given number.
def test(num):
# Count the number of ones in the binary representation of the number.
ones = bin(num).replace("0b", "").count('1')
# Count the number of zeros in the binary representation of the number.
zeros = bin(num).replace("0b", "").count('0')
# Return a formatted string containing the counts of ones and zeros.
return "Number of zeros: " + str(zeros) + ", Number of ones: " + str(ones)
# Provide a value for the first test case (n=12).
n = 12
# Print information about the first test case.
print("Original number: ", n)
# Print the result of counting ones and zeros in the binary representation of the given number using the 'test' function.
print("Number of ones and zeros in the binary representation of the said number:")
print(test(n))
# Provide a value for the second test case (n=1234).
n = 1234
# Print information about the second test case.
print("\nOriginal number: ", n)
# Print the result of counting ones and zeros in the binary representation of the given number using the 'test' function.
print("Number of ones and zeros in the binary representation of the said number:")
print(test(n))
Sample Output:
Original number: 12 Number of ones and zeros in the binary representation of the said number: Number of zeros: 2, Number of ones: 2 Original number: 1234 Number of ones and zeros in the binary representation of the said number: Number of zeros: 6, Number of ones: 5
Explanation:
Here is a breakdown of the above Python code:
- Define Test Function (test function):
- The "test()" function is defined to count the number of ones and zeros in the binary representation of a given number.
- It uses the 'bin' function to get the binary representation of the number as a string.
- The 'replace' method is used to remove the "0b" prefix from the binary representation.
- The 'count' method is used to count the occurrences of '1' and '0'.
- The counts are stored in variables and then formatted into a string for the result.
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to check whether a given month and year contains a Monday 13th.
Next: Write a Python program to find all the factors of a given natural number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/basic/python-basic-1-exercise-131.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics