{"id":19149,"date":"2021-08-29T15:13:39","date_gmt":"2021-08-29T09:43:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=19149"},"modified":"2021-11-22T18:37:08","modified_gmt":"2021-11-22T13:07:08","slug":"python-program-to-find-out-how-many-1-and-0-in-a-given-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-out-how-many-1-and-0-in-a-given-number\/","title":{"rendered":"Python Program to Find out How many 1 and 0 in a Given Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Enter Basic Salary and Calculate Gross Salary of an Employee<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to calculate the count of 1’s and 0’s in a given Number:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Enter Basic Salary and Calculate Gross Salary of an Employee Given a Number and the task is to calculate the count of 1’s and 0’s in a given Number in Python. Examples: Example1: Input: Given Number = 111000101 Output: The Count of 0’s in a …<\/p>\n
\nGiven a Number and the task is to calculate the count of 1’s and 0’s in a given Number in Python.<\/p>\nGiven Number = 111000101<\/pre>\n
The Count of 0's in a given number = 4\r\nThe Count of 1's in a given number = 5<\/pre>\n
Given Number = 1010101110000<\/pre>\n
The Count of 0's in a given number = 7\r\nThe Count of 1's in a given number = 6<\/pre>\n
Program to Find out How many 1 and 0 in a Given Number In Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_num = 111000101\r\n# Take a variable say ones_cnt and initialize its value with 0.\r\nones_cnt = 0\r\n# Take another variable say zeros_cnt\u00a0and initialize its value with 0.\r\nzeros_cnt = 0\r\n# Loop till the given number becomes zero using the while loop.\r\nwhile gvn_num:\r\n # Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).\r\n # Store it in another variable.\r\n remindr = gvn_num % 10\r\n# Calculate the value of the given number divided by 10 (which removes the last digit of the given number).\r\n# Store it in the same variable gvn_number.\r\n gvn_num = int(gvn_num\/10)\r\n# Check if the above-obtained value of the given number modulus 10 is equal to 1\r\n# using the if conditional statement.\r\n if remindr == 1:\r\n # If the statement is true, then increment the value of ones_cnt by 1 and\r\n # store it in the same variable.\r\n ones_cnt = ones_cnt+1\r\n# Check if the above-obtained value of the given number modulus 10 is equal to 0\r\n# using the if conditional statement.\r\n if remindr == 0:\r\n # If the statement is true, then increment the value of zeros_cnt\u00a0by 1 and store it in\r\n # the same variable.\r\n zeros_cnt = zeros_cnt+1\r\n # Print the values of zeros_cnt ,ones_cnt to get the count of 1's and 0's in\r\n# a given Number.\r\nprint(\"The Count of 0's in a given number = \", zeros_cnt)\r\nprint(\"The Count of 1's in a given number = \", ones_cnt)\r\n<\/pre>\n
The Count of 0's in a given number = 4\r\nThe Count of 1's in a given number = 5<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_num = int(input(\"Enter some random Number = \"))\r\n# Take a variable say ones_cnt and initialize its value with 0.\r\nones_cnt = 0\r\n# Take another variable say zeros_cnt\u00a0and initialize its value with 0.\r\nzeros_cnt = 0\r\n# Loop till the given number becomes zero using the while loop.\r\nwhile gvn_num:\r\n # Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).\r\n # Store it in another variable.\r\n remindr = gvn_num % 10\r\n# Calculate the value of the given number divided by 10 (which removes the last digit of the given number).\r\n# Store it in the same variable gvn_number.\r\n gvn_num = int(gvn_num\/10)\r\n# Check if the above-obtained value of the given number modulus 10 is equal to 1\r\n# using the if conditional statement.\r\n if remindr == 1:\r\n # If the statement is true, then increment the value of ones_cnt by 1 and\r\n # store it in the same variable.\r\n ones_cnt = ones_cnt+1\r\n# Check if the above-obtained value of the given number modulus 10 is equal to 0\r\n# using the if conditional statement.\r\n if remindr == 0:\r\n # If the statement is true, then increment the value of zeros_cnt\u00a0by 1 and store it in\r\n # the same variable.\r\n zeros_cnt = zeros_cnt+1\r\n # Print the values of zeros_cnt ,ones_cnt to get the count of 1's and 0's in\r\n# a given Number.\r\nprint(\"The Count of 0's in a given number = \", zeros_cnt)\r\nprint(\"The Count of 1's in a given number = \", ones_cnt)\r\n<\/pre>\n
Enter some random Number = 1001101\r\nThe Count of 0's in a given number = 3\r\nThe Count of 1's in a given number = 4<\/pre>\n
\n