# Python Program to Find out How many 1 and 0 in a Given Number

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 given number =  4
The Count of 1's in a given number =  5

Example2:

Input:

Given Number = 1010101110000

Output:

The Count of 0's in a given number =  7
The Count of 1's in a given number =  6

## Program to Find out How many 1 and 0 in a Given Number In Python

Below are the ways to calculate the count of 1’s and 0’s in a given Number:

### Method #1: Using While Loop (Static Input)

Approach:

• Give the number as static input and store it in a variable.
• Take a variable say ones_cnt and initialize its value with 0.
• Take another variable say zeros_cnt and initialize its value with 0.
• Loop till the given number becomes zero using the while loop.
• Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).
• Store it in another variable.
• Calculate the value of the given number divided by 10 (which removes the last digit of the given number).
• Store it in the same variable gvn_number.
• Check if the above-obtained value of the given number modulus 10 is equal to 1 using the if conditional statement.
• If the statement is true, then increment the value of ones_cnt by 1 and store it in the same variable.
• Check if the above-obtained value of the given number modulus 10 is equal to 0 using the if conditional statement.
• If the statement is true, then increment the value of zeros_cnt by 1 and store it in the same variable.
• Print the values of zeros_cnt, ones_cnt to get the count of 1’s and 0’s in a given Number.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_num = 111000101
# Take a variable say ones_cnt and initialize its value with 0.
ones_cnt = 0
# Take another variable say zeros_cnt and initialize its value with 0.
zeros_cnt = 0
# Loop till the given number becomes zero using the while loop.
while gvn_num:
# Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).
# Store it in another variable.
remindr = gvn_num % 10
# Calculate the value of the given number divided by 10 (which removes the last digit of the given number).
# Store it in the same variable gvn_number.
gvn_num = int(gvn_num/10)
# Check if the above-obtained value of the given number modulus 10 is equal to 1
# using the if conditional statement.
if remindr == 1:
# If the statement is true, then increment the value of ones_cnt by 1 and
# store it in the same variable.
ones_cnt = ones_cnt+1
# Check if the above-obtained value of the given number modulus 10 is equal to 0
# using the if conditional statement.
if remindr == 0:
# If the statement is true, then increment the value of zeros_cnt by 1 and store it in
# the same variable.
zeros_cnt = zeros_cnt+1
# Print the values of zeros_cnt ,ones_cnt to get the count of 1's and 0's in
# a given Number.
print("The Count of 0's in a given number = ", zeros_cnt)
print("The Count of 1's in a given number = ", ones_cnt)


Output:

The Count of 0's in a given number =  4
The Count of 1's in a given number =  5

### Method #2: Using While loop (User Input)

Approach:

• Give the number as user input using the int(input()) function and store it in a variable.
• Take a variable say ones_cnt and initialize its value with 0.
• Take another variable say zeros_cnt and initialize its value with 0.
• Loop till the given number becomes zero using the while loop.
• Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).
• Store it in another variable.
• Calculate the value of the given number divided by 10 (which removes the last digit of the given number).
• Store it in the same variable gvn_number.
• Check if the above-obtained value of the given number modulus 10 is equal to 1 using the if conditional statement.
• If the statement is true, then increment the value of ones_cnt by 1 and store it in the same variable.
• Check if the above-obtained value of the given number modulus 10 is equal to 0 using the if conditional statement.
• If the statement is true, then increment the value of zeros_cnt by 1 and store it in the same variable.
• Print the values of zeros_cnt, ones_cnt to get the count of 1’s and 0’s in a given Number.
• The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_num = int(input("Enter some random Number = "))
# Take a variable say ones_cnt and initialize its value with 0.
ones_cnt = 0
# Take another variable say zeros_cnt and initialize its value with 0.
zeros_cnt = 0
# Loop till the given number becomes zero using the while loop.
while gvn_num:
# Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).
# Store it in another variable.
remindr = gvn_num % 10
# Calculate the value of the given number divided by 10 (which removes the last digit of the given number).
# Store it in the same variable gvn_number.
gvn_num = int(gvn_num/10)
# Check if the above-obtained value of the given number modulus 10 is equal to 1
# using the if conditional statement.
if remindr == 1:
# If the statement is true, then increment the value of ones_cnt by 1 and
# store it in the same variable.
ones_cnt = ones_cnt+1
# Check if the above-obtained value of the given number modulus 10 is equal to 0
# using the if conditional statement.
if remindr == 0:
# If the statement is true, then increment the value of zeros_cnt by 1 and store it in
# the same variable.
zeros_cnt = zeros_cnt+1
# Print the values of zeros_cnt ,ones_cnt to get the count of 1's and 0's in
# a given Number.
print("The Count of 0's in a given number = ", zeros_cnt)
print("The Count of 1's in a given number = ", ones_cnt)


Output:

Enter some random Number = 1001101
The Count of 0's in a given number = 3
The Count of 1's in a given number = 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.