Question

Write a program that prompts the user to enter a binary string and displays the corresponding...

Write a program that prompts the user to enter a binary string and displays the corresponding decimal integer value.

For example, binary string ‘10001’ is 17 (1*24 +0*23 +0*22 +0*2 + 1 = 17)

A sample run :

Enter a binary: 10001

17

Enter second integer: 110

6

Use python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

binary_string = input('Enter binary : ')

def converter(binary_string):

    current_value = 2**(len(binary_string)-1)
    total = 0
    for i in binary_string:
      if i is '1':
            total += current_value
elif i is '0':
            pass
        else:
            print('non binary string')
            return 'Error'
        current_value = current_value//2
    return total

print(converter(binary_string))

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user to enter a binary string and displays the corresponding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to enter a file name and displays the occurrences...

    Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the file. Letters are case-insensitive. Here is a sample run: Enter a filename: Lincoln.txt Number of A’s: 23 Number of B’s: 0 Number of C’s: 12 …… Number of Y’s: 5 Number of Z’s: 7

  • (Process a string) Write a program that prompts the user to enter a string and displays...

    (Process a string) Write a program that prompts the user to enter a string and displays its length and its first character. java StringLength Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic? The string is of length 88 and the first character is D import java.util.Scanner; class StringLength{    public static void main (String args[]){        Scanner input = new Scanner(System.in);        System.out.println("Enter a string:");        String ans = input.nextLine();        System.out.println("The string...

  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter...

    IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter a string and displays the maximum consecutive increasingly ordered sub-string. Analyze the time complexity of your program and explain. Here is a sample run: Enter a string: abcabcdgabxy output abcdg Time complexity is O( ). -n must be display between the parenthesis.

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • Design a program that prompts the user to enter a string, and displays the character that...

    Design a program that prompts the user to enter a string, and displays the character that appears most frequently in the string. (Please read the problem above carefully. I'm asking this question for the second time because the last answer I received wasn't even paying attention to what the question asked for.) Answer MUST be using Flowgorithm with screenshots. Please attach a screenshot of the program successfully running in flowgorithm as well to confirm proper output. Thanks for understanding!

  • write a java program that displays the following: Write a Java program that prompts the user...

    write a java program that displays the following: Write a Java program that prompts the user to enter an integer and determines whether 1. it is divisible by 5 and 6, whether on 2. it is divisible by 5 or 6, 3. it is divisible by 5 or 6, but not both. Here is a sample run of this program: Sample run: Enter an integer: 10 Is 10 divisible by 5 and 6? false Is 10 divisible by 5 or...

  • Write a Python program that prompts the user to enter integer values for each of two...

    Write a Python program that prompts the user to enter integer values for each of two lists. It then should displays whether the lists are of the same length, whether the elements in each list sum to the same value, and whether there are any values that occur in both lists.

  • Write a program that prompts the user for a string and stores the user's string into...

    Write a program that prompts the user for a string and stores the user's string into a variable using getline(). After receiving the string, the program counts and displays the number of vowels in the string. Then, the program should prompt the user if they would like to enter another string.

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT