 # adding two fractions in python

• Last Update :
• Techknowledgy :
```def findGCD(n1, n2):
gcd = 0
for i in range(1, int(min(n1, n2)) + 1):
if n1 % i == 0 and n2 % i == 0:
gcd = i
return gcd

# input first fraction
num1, den1 = map(int, list(input("Enter numerator and denominator of first number : ").split(" ")))

# input first fraction
num2, den2 = map(int, list(input("Enter numerator and denominator of second number: ").split(" ")))

lcm = (den1 * den2) // findGCD(den1, den2)

sum = (num1 * lcm // den1) + (num2 * lcm // den2)

num3 = sum // findGCD(sum, lcm)

lcm = lcm // findGCD(sum, lcm)

print(num1, "/", den1, " + ", num2, "/", den2, " = ", num3, "/", lcm)```

### Output

```Enter numerator and denominator of first number: 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5```

Suggestion : 2

Quants Arithmetic Algebra Number Series Time & Work More ,Technical C Algorithm Computer Architecture Computer Network More

```def findGCD(n1, n2):
gcd = 0
for i in range(1, int(min(n1, n2)) + 1):
if n1 % i == 0 and n2 % i == 0:
gcd = i
return gcd

# input first fraction
num1, den1 = map(int, list(input("Enter numerator and denominator of first number : ").split(" ")))

# input first fraction
num2, den2 = map(int, list(input("Enter numerator and denominator of second number: ").split(" ")))

lcm = (den1 * den2) // findGCD(den1, den2)

sum = (num1 * lcm // den1) + (num2 * lcm // den2)

num3 = sum // findGCD(sum, lcm)

lcm = lcm // findGCD(sum, lcm)

print(num1, "/", den1, " + ", num2, "/", den2, " = ", num3, "/", lcm)```
`Enter numerator and denominator of first number: 14 10 Enter numerator and denominator of second number: 24 314 / 10 + 24 / 3 = 47 / 5`

Suggestion : 3

A Fraction instance can be constructed from a pair of integers, from another rational number, or from a string.,The fractions module provides support for rational number arithmetic.,Alternative constructor which only accepts instances of float or numbers.Integral. Beware that Fraction.from_float(0.3) is not the same value as Fraction(3, 10).,The Fraction class inherits from the abstract base class numbers.Rational, and implements all of the methods and operations from that class. Fraction instances are hashable, and should be treated as immutable. In addition, Fraction has the following properties and methods:

```[sign] numerator['/'
denominator]```
```>>> from fractions
import Fraction
>>>
Fraction(16, -10)
Fraction(-8, 5) >>>
Fraction(123)
Fraction(123, 1) >>>
Fraction()
Fraction(0, 1) >>>
Fraction('3/7')
Fraction(3, 7) >>>
Fraction(' -3/7 ')
Fraction(-3, 7) >>>
Fraction('1.414213 \t\n')
Fraction(1414213, 1000000) >>>
Fraction('-.125')
Fraction(-1, 8) >>>
Fraction('7e-6')
Fraction(7, 1000000) >>>
Fraction(2.25)
Fraction(9, 4) >>>
Fraction(1.1)
Fraction(2476979795053773, 2251799813685248) >>>
from decimal
import Decimal
>>>
Fraction(Decimal('1.1'))
Fraction(11, 10)```
```>>> from fractions
import Fraction
>>>
Fraction('3.1415926535897932').limit_denominator(1000)
Fraction(355, 113)```
```>>> from math
import pi, cos
>>>
Fraction(cos(pi / 3))
Fraction(4503599627370497, 9007199254740992) >>>
Fraction(cos(pi / 3)).limit_denominator()
Fraction(1, 2) >>>
Fraction(1.1).limit_denominator()
Fraction(11, 10)```
```>>> from math
import floor
>>>
floor(Fraction(355, 113))
3```