54.7k views
0 votes
The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a number between 0 and 1). Complete the body of the function so that it returns the right number. Note: Since division by 0 produces an error, if the denominator is 0, the function should return 0 instead of attempting the division.

User Yung
by
4.5k points

2 Answers

2 votes

Answer:

import math

def fractional_part(num, den):

if den == 0:

return 0

return num / den - math.floor(num / den)

print(fractional_part(5,4)) // outputs 0.25

print(fractional_part(6,4)) // outputs 0.5

print(fractional_part(12,0)) // outputs 0

Step-by-step explanation:

  • Using Python Programming Language:
  • Import math (Enables the use of the math.floor function)
  • Create an if statement to handle situations of denominator equals to zero
  • Dividing the floor (lower bound) of the numerator (num) by the denominator (den) and subtracting from the value of num / den gives the fractional part.
User Moogal
by
4.1k points
4 votes

Answer:

def fractional_part(numerator,denominator):

if denominator == 0:

return 0

else :

return (numerator/denominator)%1

Step-by-step explanation:

User Jdoroy
by
4.0k points