Question

NOTE: Write the Program in python as mentioned below and use while loop and flags as...

NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below.

DON'T use Function concept or any other concept except while loop,if-else.

Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not receive any credit, so be sure that your code interprets correctly before submission. Remember: It is important that you test your code thoroughly before submission to ensure that your program is correct.

1 Program Description A binary number (or a base 2 number) is a number expressed using only the digits 0 and 1. Each individual digit of a binary number is called a bit. A string consisting of only the symbols 0 and 1 are called bit strings. Example: The binary number 1101 represents the decimal number 13, and consists of 4 bits. All data is represented by the computer as binary numbers. Thus, operations on binary numbers are extremely important in computer science. In this project you will write an application that converts binary numbers to their base 10 equivalent. In particular, your program must prompt the user to enter a binary number (represented as a bit string), and read the string from the user. Your program must then display the corresponding base 10 number to the user. The format of your program’s output must match the following: (s)2 = (x)10 where s is the binary number read from the user, and x is the base 10 value of the number. No other output should be produced by your program. Example: If the user enters the binary number 1101 then your program must output: (1101)2 = (13)10

2 Converting Base 2 to Base 10 Let b = bnbn−1bn−2· · ·b2b1b0 be a binary number where each bit bi is either 0 or 1. The following formula can be used to convert a given binary number to its corresponding base 10 equivalent: (b)2 = (b02 0 + b12 1 + b22 2 + · · · + bn−12 n−1 + bn2 n )10 Your program must use the above formula, along with an appropriate loop, to compute the base 10 value of a given binary number read from the user. Solutions that do not include a loop that computes the above formula will receive zero credit.

3 Additional Requirements Your program must also check for the following fatal runtime error(s): 1. If the user enters a string that does not represent a valid base 2 number, then your program must print a suitable error message and exit. Invalid input, for this program, is any string that contains characters besides 0 and 1. No other errors need to be accounted for by your program. Page 1 of 2

4 Program Outline The following is a logical outline of the program requirements.

1 s = read string representing a binary number.

2

3 if s is not a bit string then

4 display error message.

5 else

6 convert s to its base 10 equivalent .

7 display result to the user.

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

Solution:

s=input("Enter binary number:") #read input from user
length=len(s)
l=list(s)
if l.count('0')+l.count('1')!=len(s):#check validity of input
print("Invalid input")
exit(0)
else:
l.reverse()
s=''.join(l)
l.reverse()
res,i=0,0
while(length!=0):
length-=1
res=res+int(s[i])*pow(2,i) #find the decimal representation
i+=1

print("("+''.join(l)+")"+str(2)+"="+"("+str(res)+")"+str(10))#print the result in desired format on console

Screenshots:

The screenshots are attached below for reference.

Please follow them for proper indentation.

Add a comment
Know the answer?
Add Answer to:
NOTE: Write the Program in python as mentioned below and use while loop and flags as...
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
  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not receive any credit, so be sure that your code interprets correctly...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

  • Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in...

    Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this...

    Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

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