CBSE Class 11 Informatics Practices Question 28 of 36

Conditional and Looping Constructs — Question 28

Back to all questions
28
Question

Question 12

WAP to convert binary number to decimal number.

Solution
binary = input("Enter a binary number: ")
decimal = 0
for digit in binary:
    decimal = decimal * 2 + int(digit)

print("The decimal equivalent of", binary, "is", decimal)
Output
Enter a binary number: 110
The decimal equivalent of 110 is 6   
Answer

binary
=
input
(
"Enter a binary number: "
)
decimal
=
0
for
digit
in
binary
:
decimal
=
decimal
*
2
+
int
(
digit
)
print
(
"The decimal equivalent of"
,
binary
,
"is"
,
decimal
)
Output
Enter a binary number: 110
The decimal equivalent of 110 is 6