Question

Converting a decimal to a fraction 10 points Although computers generally represent numbers as decimals, it is often convenient to express values as fractions. If we desire to convert a decimal to a fraction, we need to follow a two-step process .Starting with the number itself, multiply by ten repeatedly until no values remain after the decimal point. This is the numerator. Keep the multiplier as the denominator 2. Simplify the fraction. For instance, for 0.75: 1. Multiply by ten repeatedly: 0.75 x 10x 10 75 Thus the numerator is 75 and the denominator is 10x 10 100 2. Simplify the fraction: 1.Find the factors of the numerator and the denominator. 75 (1.3,5.25,75) 100,2,4,5, 10,20,25,50, 100] 2. Find the greatest common factor of each and divide both by it 75+25 10025 4 0.75 4python 3

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

Please do rate the answer thanks,..

Executable Code:
---------------------------------------------------------------------------------------------------

# function to find the factors of a number
def factors(n):

    # empty list
    t=[]

    # looping
    for i in range(1,n+1):

        # if n is divisible by i , then i is a factor of n
        if n % i == 0:
            t.append(i)
          
    # returning the list
    return t

# function to return the fraction of a decimal number
def fraction(n):

    # counter
    mul_count=0

    # converting the decimal number to string
    s=str(n)

    # temporary variable
    t=0

    # finding the index of '.'
    index=s.index('.')

    # looping
    while s[index+1]!='0':   

        # converting string to float
        t=float(s)

        # multiplying the number by 10
        t=t*10

        # incrementing the counter
        mul_count+=1

        # converting the number to string
        s=str(t)

        # finding the index of '.'
        index=s.index('.')

    # getting the numerator
    numerator=int(t)

    # variable for denominator
    denominator= 1

    # finding the denomiantor
    for i in range(mul_count):
        denominator*=10

    # finding the factors numerator and denomiantor
    f_numerator=factors(numerator)
    f_denominator=factors(denominator)


    # finding the gcf of two list
    a=set(f_numerator)
    b=set(f_denominator)

    gcf=max( a & b)

    # returning the fraction
    return (int(numerator/gcf),int(denominator/gcf))

# testing
if __name__=='__main__':

    print(fraction(0.750))
    print(fraction(0.5))
    print(fraction(0.250))
------------------------------------------------------------------------------------------------------


SCREENSHOT FOR OUTPUT:

4241

Add a comment
Know the answer?
Add Answer to:
python 3 Converting a decimal to a fraction 10 points Although computers generally represent numbers 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
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