From 1c6559866648601e30808cbd932b572510edf8b4 Mon Sep 17 00:00:00 2001 From: Ayesh Biswal Date: Mon, 3 Oct 2022 19:10:20 +0530 Subject: [PATCH] Add program that converts binary number to decimal --- binary_to_decimal.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 binary_to_decimal.py diff --git a/binary_to_decimal.py b/binary_to_decimal.py new file mode 100644 index 0000000..d8e8970 --- /dev/null +++ b/binary_to_decimal.py @@ -0,0 +1,21 @@ +while True: + try: + temp = int(input('Binary num: ')) + binary = temp + while temp > 0: + if temp % 10 != 0 and temp % 10 != 1: + raise + temp //= 10 + break + except: + print('Invalid Binary Number!') + pass + +decimal = 0 +position = 0 +while binary > 0: + digit = binary % 10 + decimal += digit * (2**position) + binary //= 10 + position += 1 +print(decimal)