Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85a1152

Browse files
Merge pull request #23 from heyyayesh/binary-to-decimal
Add program that converts binary number to decimal
2 parents 3df3b94 + 1c65598 commit 85a1152

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎binary_to_decimal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
while True:
2+
try:
3+
temp = int(input('Binary num: '))
4+
binary = temp
5+
while temp > 0:
6+
if temp % 10 != 0 and temp % 10 != 1:
7+
raise
8+
temp //= 10
9+
break
10+
except:
11+
print('Invalid Binary Number!')
12+
pass
13+
14+
decimal = 0
15+
position = 0
16+
while binary > 0:
17+
digit = binary % 10
18+
decimal += digit * (2**position)
19+
binary //= 10
20+
position += 1
21+
print(decimal)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /