Binary to Decimal converter:
Introduction To Binary to decimal Converter
Binary to decimal converter. Hello coder in this blog we will discuss something Elementry this is quite basic to your computer knowledge, We will discuss the Binary to decimal conversion. If you want to know more about the Binary and decimal number systems Do comment below I will make a separate blog on this topic.
Steps:
1) Write down all powers of 2 starting from rightmost starting from 20 And so on like -2) Now multiply each digit in form of binary numbers starting from the right with respect to their numeric positions given above
3) Sum up all the products obtained by multiplying binary numbers with their power of 2.
Now following the same logic we write the algorithm for our code for Binary to decimal converter
It's a simple well-commented code for your Ease
If you have any queries do comment below. keep coding
ALL THE BEST!!
def binaryToDecimal(n): num = n; dec_value = 0; # Initializing base # value to 1, i.e 2 ^ 0 base = 1; temp = num; while(temp): last_digit = temp % 10; temp = int(temp / 10); dec_value += last_digit * base; base = base * 2; return dec_value; # Driver Codenum = int(input("Enter the Binary number"));print(binaryToDecimal(num));
def binaryToDecimal(n):
num = n;
dec_value = 0;
# Initializing base
# value to 1, i.e 2 ^ 0
base = 1;
temp = num;
while(temp):
last_digit = temp % 10;
temp = int(temp / 10);
dec_value += last_digit * base;
base = base * 2;
return dec_value;
# Driver Code
num = int(input("Enter the Binary number"));
print(binaryToDecimal(num));
Superb👍🏻
ReplyDelete