How to convert a binary number to decimal in python


Asked by admin @ in Computers and Technology viewed by 331 People


Write a Python program that translates a binary number of n bits to a decimal number. Your program should first ask the user the length of the binary number (how many bits). Using that information, ask the user to provide each bit from lowest to highest. Once the user has entered all the bit values for the binary number, report back the decimal value. Hint: You should use the mechanism to convert binary to decimal in earlier lectures. Note: Your program should not use any functions or methods from any library.

Answered by admin @



Answer:

Follows are the code to this question:

n= int(input("please enter number of bits, which want to convert for binary to decimal numbers: "))

p= 1#defining variable p for calculate power

d= 0#defining variable d for calculate decimal value

for i in range(n):#defining for loop for input bits value

   b= int(input("Enter the bit value from lowest to highest: "))#input value in b

   if b==1:#defining if block that check input is 1 value

       d=d+p#calculating decimal value

   p=p* 2#calculating power

print("The converted decimal number is:",d)#print value

Output:

please find the attached file.

Explanation:

In the above-given code, n variable is defined, which takes the number of bits value which you want to convert into a decimal value.

In the next step, "d and p" variable is defined that calculates the power and decimal number and store its value respectively, in this step, a for loop is used that uses the "b" variable for inputs bits value into lowest to the highest form, and use the if block.

In this block, it checks if the input value is one then it calculates its power and store its value into the "d" variable and use the print method to print its value.  


Similar Questions

Convert the following decimal numbers into binary numbers

Asked by admin @ in Computer Science viewed by 365 persons

Convert the following decimals numbers into binary numbers (25)10​

Convert the following binary numbers to hexadecimal and to decimal

Asked by admin @ in Computer Science viewed by 360 persons

6. Convert the following binary numbers to decimal and hexadecimal : (a) 1010 (6) 111010 (c) 101011111(d) 1100 (e) 10010101 (1) 11011100what will be the conversion of the …

How do you convert a rational number to a decimal

Asked by admin @ in Math viewed by 596 persons

How to convert rational numbers into decimals?

Decimal number 10 is equal to binary number

Asked by admin @ in Math viewed by 330 persons

Decimal number 10 is equal to binary number a. 1110 b. 1010 c. 1001 d. 1000

Convert the hexadecimal number 1e2 16 to decimal

Asked by admin @ in Math viewed by 515 persons

Convert the hexadecimal number (1E2)16 to decimal * 480481482483​

Difference between binary number system and decimal number system

Asked by admin @ in Math viewed by 336 persons

Difference between decimal system and binary system

The binary equivalent of the decimal number 10 is

Asked by admin @ in Computer Science viewed by 294 persons

The binary equivalent of the decimal number 10 is​

The binary number 10101 is equivalent to decimal number

Asked by admin @ in Computer Science viewed by 360 persons

Q1. The binary number 10101 is equivalent to decimal number1. 192. 123. 274. 21​

Convert the following numbers with the indicated bases to decimal

Asked by admin @ in Engineering viewed by 482 persons

I. Convert the following numbers with the indicated bases to decimal: 1. (1O111O)2 2. (1110101.11)2 3. (12121)3 4. (4310)5 5. (50)7 6. (198)12

How do you convert a repeating decimal to a fraction

Asked by admin @ in Mathematics viewed by 295 persons

Explain the steps on how to convert a repeating decimal to a fraction

Square root of a number by newton's method in python

Asked by admin @ in Computers and Technology viewed by 307 persons

Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of the square root should be passed as a second argument …

How do you convert a repeating decimal into a fraction

Asked by admin @ in Mathematics viewed by 334 persons

Convert the repeating decimal to a fraction. 0.81 =

Python generate array of random numbers between 0 and 1

Asked by admin @ in Computers and Technology viewed by 310 persons

Write a for loop in python that prints 10 random numbers between 1 and 100 inclusive.

What is the base of decimal number system

Asked by admin @ in Computer Science viewed by 396 persons

What is the base of decimal number system​

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4435 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 610 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 599 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 579 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 576 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 567 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 540 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 518 persons