Question

Convert a decimal fractional number 34/5 to its binary equivalent using a program to  evaluate the bits...

Convert a decimal fractional number 34/5 to its binary equivalent using a program to  evaluate the bits of the fractional part for at least upto the second repeating block.

i have written some code in python however it is not getting an output at all, so either fixing the code or writing another one would be beyond helpful

def binary(n,k):
n = 8.6
k = 3
#'n' is the fractional number
#'k' is the number of bits up to the loop ;
integral = int(n)
fraction = n-integral

b = '.'
while(k):
fraction = fraction * 2
fra_num = int(fraction)
if (frac_num == 1):
fraction = fraction - frac_num
b = b + '1'
else:
b = b + '0'
k = k - 1
return b

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

def Binary(num, k_prec) :

    binary = ""

    Integral = int(num)  

    fractional = num - Integral

    while (Integral) :

        rem = Integral % 2

        binary += str(rem);

        Integral //= 2

    binary = binary[ : : -1]

    binary += '.'

    while (k_prec) :  

        fractional *= 2

        fract_bit = int(fractional)

        if (fract_bit == 1) :

            fractional -= fract_bit

            binary += '1'

        else :

            binary += '0'

        k_prec -= 1

    return binary

if __name__ == "__main__" :

    n = 8.6

    k = 8

    print(Binary(n, k))

Output:

IF YOU UNDERSTOOD IT CLEARLY, PLEASE LIKE/UPVOTE

OTHERWISE, FEEL FREE TO ASK IN COMMENT SECTION

I WILL SURELY HELP YOU!! THANK YOU!!

Add a comment
Know the answer?
Add Answer to:
Convert a decimal fractional number 34/5 to its binary equivalent using a program to  evaluate the bits...
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
  • The code is to convert the fractional part of a decimal number to its hexadecimal form....

    The code is to convert the fractional part of a decimal number to its hexadecimal form. Can you explain the code, for example ,why the fractional part need to multiply 16 instead of devise 16? FracNum DecNum-num int count, IntPart float FracPart; float temp temp-FracNum *16; FracPartstemp- (int) temp; //getting Fractional part from temp IntPart (int) temp; count-e ise while (FracPart >e && count<=6) //while fractional part e and count Less than or equal to 6 then //Now taking fractional...

  • a) The following program is supposed to convert a binary number to its decimal equivalent. Modify...

    a) The following program is supposed to convert a binary number to its decimal equivalent. Modify the program so that it does the job.(15 points) b) How would you create the executable file of the program (file name: b2d.c) in Linux? (5 points) #include <stdio.h> int main (void) int binary: printf("Enter a binary number:\n"); scanf("%d", &binary): int nofDigit = 0, remain = binary: while (remain > 0) - remain = remain/10; nofDigit++; int digit, dval = 0, bx = binary:...

  • Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we...

    Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal? Given a binary string of an arbitrarily length k, bk-1....bi .box the decimal number can be computed by the formula 20 .bo +21.b, + ... + 2k-1. bx-1- In mathematics, we use the summation notation to write the above formula: k- 2.b; i=0) In a program,...

  • [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g` `Incorrect hex number` Hints: you...

  • 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...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • 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...

  • Question 2: Levenshtein Take the recursive Java program Levenshtein.java and convert it to the equivalent C...

    Question 2: Levenshtein Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. Notes You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions provided in assignment 0. Assertions are disabled by default. You can enable assertions...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

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