The purpose of this question is to write a complete python program that computes
ID: 3908848 • Letter: T
Question
The purpose of this question is to write a complete python program that computes the product of two fractions represented by large integers. Each fraction must be to the same number of decimal places. The number of decimal places must be a positive integer. There must not be any floats in this program! Instead of representing a fraction as x/y where x and y are integers with y <> 0, you represent it as a single integer z, with a fixed denominator B. You obtain B from the number of decimal places entered by the user. For example, for 50 decimal places, B = 10**50.
Write a function that begins with the following header:
def getPositiveInt(prompt):
This function is given:
• prompt - the string input is to display to the user when requesting input.
Valid input from the user is an integer greater than zero.
Input an integer from the user. Loop as long as the integer is less than or equal to zero. In the loop display the value of the integer followed by the text 'is not greater than zero'. Then input another integer from the user. When the integer entered by the user is valid return the integer.
Write a function that begins with the following header:
def getFraction(one):
This function is given:
• one - the large integer value that represents 1.0
Input integer values for the numerator and denominator of the fraction. Loop as long as the denominator is equal to zero and display the message 'The denominator cannot be zero!'. Return the large integer that represents the fraction.
Note: Be sure to handle the special case that occurs when the fraction is negative.
Write a function that begins with the following header:
def multiplyFractions(fraction1, fraction2, one):
This function is given:
• fraction1 - the multiplicand
• fraction2 - the multiplier
• one - the large integer value that represents 1.0
Compute and return the product of fraction1 and fraction2 as a large integer.
Write a function that begins with the following header:
def displayFraction(fraction, places):
This function is given:
• fraction - the large integer that represents the fraction to display
• places - the number of decimal places to which the fraction is to be displayed
Display the large integer that represents the fraction to the given number of decimal places. If the fractional part of the fraction is zero, display only the integer part of the fraction.
Write a function that begins with the following header:
def main():
This function:
• calls getPositiveInt to input the number of decimal places.
• computes the large integer value that represents 1.0
• calls getFraction to input the first fraction
• loops as long as the first fraction is not zero
o calls displayFraction to display the first fraction
o calls getFraction to input the second fraction
o calls displayFraction to display the second fraction
o calls multilpyFractions to get the product of the two fractions
o calls displayFraction to display the product of the two fractions
o calls getFraction to input the first fraction
• calls displayTerminationMessage to display the termination message
The main program (not to be confused with function named main) should contain the function definitions, any import statements needed by the functions and a call to the function named main.
Explanation / Answer
here is your program : ------------>>>>>>>>>
import math
def getPositiveInt(prompt):
n = -1
while True:
n = int(input(prompt))
if n <= 0:
print(str(n)," is Not greater than 0 ")
else:
return n;
def getFraction(one):
n = int(input("Enter a Integer Numerator : "))
n1 = int(input("Enter a Integer Denominator : "))
while n1 == 0:
print(str(n1)," Denominator can not be zero ! ")
n1 = int(input("Enter a Integer Denominator : "))
n = n * one;
n = int(n/n1);
return n;
def displayFraction(fraction,places):
n = float(fraction/math.pow(10,places))
print(n)
def multiplyFractions(frac1,frac2,one):
n = int(((frac1/one)*(frac2/one))*one)
return n
def main():
n = getPositiveInt("Enter a Integer Number for decimal Place :")
> n1 = getFraction(one)
displayFraction(n1,n)
n2 = getFraction(one)
displayFraction(n2,n)
n3 = multiplyFractions(n1,n2,one)
displayFraction(n3,n)
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.