Question

In: Computer Science

In the decimal system (base 10), a natural number is represented as a sequence dndn−1 ....

In the decimal system (base 10), a natural number is represented as a sequence dndn−1 . . . d0 of (decimal) digits, each of which is in the range 0..9. The value of the number is d0 ×100 +d1 ×101 +···+ dn ×10n. Similarly, in the binary system (base 2), a natural number is represented as a sequence bnbn−1 · · · b0 of (binary) digits, each of which is 0 or 1. The value of the number is b0 ×20 +b1 ×21 +···+bn ×2n. For example, the value of the number whose binary representation is 101is: 1×20+0×21+1×22 =1+0+4=5.

Without using any built-in functions for converting numbers, write a function that takes as input the binary representation of a positive integer n and returns its decimal representation.

Solutions

Expert Solution

# Python function to take the binary input and return its decimal notation
#####Function starts here
def convertToDecimal(bina):
n=len(bina)
res=0
for i in range(1,n+1):
    res=res+ int(bina[i-1])*2**(n-i)
return res
####Function ends here

## Driver program for the function
binary=[]
binary = input ('Enter binary to be converted: ')
decimal= convertToDecimal(binary)
print("Decimal Equivalanet is ", decimal)

Output Screenshot:


Code:


Related Solutions

convert the binary number(base 2) To Octal (base 8) to decimal (base 10) a. 101 b....
convert the binary number(base 2) To Octal (base 8) to decimal (base 10) a. 101 b. 1001 c. 101010 d.1101101 convert the number to the other base a. 253 base 10 to base 8 b. 98 base 10 to base 3 C. 1340 base 10 to base 16 D. AB Base 16 to base 8 E. 111010 base 2 to base 16 F. 1010101 base 2 to base 6 g. 69 base 10 to base 2 h . 1023 base...
What base 10 number (using powers of 10 if using scientific notation) is represented by the...
What base 10 number (using powers of 10 if using scientific notation) is represented by the following IEEE 754 hexadecimal representation? a. 40 70 00 00 b. FF E4 00 00 c. 80 20 00 00
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system to base-16 number system Convert 100 from base-10 number system to base-2 number system Convert 100 from base-10 number system to base-16 number system Convert ef from base-16 number system to base-2 number system Convert ef from base-16 number system to base-10 number system
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
The number –11.375 (decimal) represented as a 32-bit floating-point binary number according to the IEEE 754...
The number –11.375 (decimal) represented as a 32-bit floating-point binary number according to the IEEE 754 standard is
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
What decimal number is represented by the binary 2’s complement 8-bit number 11100111? Show your work
What decimal number is represented by the binary 2’s complement 8-bit number 11100111? Show your work
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
We usually write numbers in decimal form (or base 10), meaning numbers are composed using 10...
We usually write numbers in decimal form (or base 10), meaning numbers are composed using 10 different “digits” {0,1,...,9}. Sometimes though it is useful to write numbers in hexadecimal or base 16. Now there are 16 distinct digits that can be used to form numbers: {0,1,...,9,A,B,C,D,E,F}. So for example, a 3 digit hexadecimal number might be 3B8. (a) How many 2-digit hexadecimals are there in which the first digit is E or F? Explain your answer in terms of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT