53.0k views
5 votes
Write a function addFractions that takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. Note that this function should have a total of six parameters. Hint: a/b + c/d = (ad+bc)/bd.

User Sakeer
by
5.8k points

1 Answer

2 votes

Answer:

The function is written in Python:

def addFractions(a,b,c,d):

num = a*d + b*c

denom = b * d

return [num, denom]

a = int(input("Numerator 1: "))

b = int(input("Denominator 1: "))

c = int(input("Numerator 2: "))

d = int(input("Denominator 2: "))

print(addFractions(a,b,c,d))

Step-by-step explanation:

The program is written in Python and I also include the main of the program

This defines the function

def addFractions(a,b,c,d):

This calculates the numerator

num = a*d + b*c

This calulates the denominator

denom = b * d

This returns the numerator and the denominator

return [num, denom]

The main starts here

This prompts user for the numerator of the first fraction

a = int(input("Numerator 1: "))

This prompts user for the denominator of the first fraction

b = int(input("Denominator 1: "))

This prompts user for the numerator of the second fraction

c = int(input("Numerator 2: "))

This prompts user for the denominator of the second fraction

d = int(input("Denominator 2: "))

This pass arguments to the function

print(addFractions(a,b,c,d))

User ScottyB
by
6.1k points