Convert binary to int python: In the previous article, we have discussed Python Program to Convert a Decimal Number to Binary
Given a binary number and the task is to get the respective decimal number using the while loop.
Examples:
Example1:
Input:
Given Binary Number = 1000
Output:
The Decimal value of the given binary number { 1000 } is: 8
- Python Program to Convert Octal to Binary
- Python Program to Convert Hexadecimal to Decimal
- Python Program to Convert Hexadecimal To Octal
Example2:
Input:
Given Binary Number = 1100
Output:
The Decimal value of the given binary number { 1100 } is: 12
Program to Convert Binary to Decimal using While Loop in Python
Python convert to binary: Below are the ways to convert the given binary number into decimal :
Method #1: Using While Loop (Static Input)
Approach:
- Give the binary number as static input and store it in a variable.
- Take a variable say ‘a ‘and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given binary number modulus 10 (to get the last digit) and store it in a variable say rem.
- Calculate the value of the given binary number divided by 10.
- Store it in the same variable gvn_binry_num.
- Multiply rem with the 2 power a using the pow() function and store it in another variable say ‘b’.
- Add b with the above deciml_num and store it in the same variable deciml_num.
- Increment the value a by 1 and store it in the same variable ‘a’.
- Print the deciml_num to get the decimal value of the given binary number.
- The Exit of the Program.
Below is the implementation:
# Give the binary number as static input and store it in a variable. gvn_binry_num = 1000 # Take a variable say 'a 'and initialize its value with 0. a = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 print( "The Decimal value of the given binary number {", gvn_binry_num, "} is: ") # Loop till the given number is not equal to zero using the while loop. while (gvn_binry_num != 0): # Inside the loop, calculate the value of the given binary number modulus 10 # (to get the last digit) and store it in a variable say rem. rem = gvn_binry_num % 10 # Calculate the value of the given binary number divided by 10. # Store it in the same variable gvn_binry_num. gvn_binry_num = gvn_binry_num // 10 # Multiply rem with the 2 power a using the pow() function and store it in # another variable say 'b'. b = rem*pow(2, a) # Add b with the above deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num + b # Increment the value a by 1 and store it in the same variable 'a'. a = a+1 # Print the deciml_num to get the decimal value of the given binary number. print(deciml_num)
Output:
The Decimal value of the given binary number { 1000 } is: 8
Method #2: Using While loop (User Input)
Approach:
- Give the binary number as user input using the int(input()) function and store it in a variable.
- Take a variable say ‘a ‘and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given binary number modulus 10 (to get the last digit) and store it in a variable say rem.
- Calculate the value of the given binary number divided by 10.
- Store it in the same variable gvn_binry_num.
- Multiply rem with the 2 power a using the pow() function and store it in another variable say ‘b’.
- Add b with the above deciml_num and store it in the same variable deciml_num.
- Increment the value a by 1 and store it in the same variable ‘a’.
- Print the deciml_num to get the decimal value of the given binary number.
- The Exit of the Program.
Below is the implementation of the above given approach:
# Give the binary number as user input using the int(input()) function and # store it in a variable. gvn_binry_num = int(input("Enter some Random Number = ")) # Take a variable say 'a 'and initialize its value with 0. a = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 print( "The Decimal value of the given binary number {", gvn_binry_num, "} is: ") # Loop till the given number is not equal to zero using the while loop. while (gvn_binry_num != 0): # Inside the loop, calculate the value of the given binary number modulus 10 # (to get the last digit) and store it in a variable say rem. rem = gvn_binry_num % 10 # Calculate the value of the given binary number divided by 10. # Store it in the same variable gvn_binry_num. gvn_binry_num = gvn_binry_num // 10 # Multiply rem with the 2 power a using the pow() function and store it in # another variable say 'b'. b = rem*pow(2, a) # Add b with the above deciml_num and store it in the same variable deciml_num. deciml_num = deciml_num + b # Increment the value a by 1 and store it in the same variable 'a'. a = a+1 # Print the deciml_num to get the decimal value of the given binary number. print(deciml_num)
Output:
Enter some Random Number = 011 The Decimal value of the given binary number { 11 } is: 3
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.