How to add fractions in python

I am trying to add two fractions in python

if input 1/4 + 1/4, I am expecting 1/2 result

I built a fraction class with an __add__ method for addition

from fractions import gcd

class fraction:
    def __init__[self, numerator, denominator]:
        self.num = numerator
        self.deno = denominator
    def __add__[self, other]:
        self.sumOfn = self.num + other.num
        self.sumOfd = gcd[self.deno,other.deno]
        return[self.sumOfn, self.sumOfd]



print[fraction[1,4]+fraction[1,4]]

However I am getting 2,4 as output, which is actually 1/2, just not simplified. How could I fix that problem ?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Add two fraction a/b and c/d and print answer in simplest form.
    Examples : 
     

    Input:  1/2 + 3/2
    Output: 2/1
    
    Input:  1/3 + 3/9
    Output: 2/3
    
    Input:  1/5 + 3/15
    Output: 2/5

    Algorithm to add two fractions 
     

    • Find a common denominator by finding the LCM [Least Common Multiple] of the two denominators.
    • Change the fractions to have the same denominator and add both terms.
    • Reduce the final fraction obtained into its simpler form by dividing both numerator and denominator by their largest common factor.

    C++

    #include

    using namespace std;

    int gcd[int a, int b]

    {

        if [a == 0]

            return b;

        return gcd[b%a, a];

    }

    void lowest[int &den3, int &num3]

    {

        int common_factor = gcd[num3,den3];

        den3 = den3/common_factor;

        num3 = num3/common_factor;

    }

    void addFraction[int num1, int den1, int num2,

                     int den2, int &num3, int &den3]

    {

        den3 = gcd[den1,den2];

        den3 = [den1*den2] / den3;

        num3 = [num1]*[den3/den1] + [num2]*[den3/den2];

        lowest[den3,num3];

    }

    int main[]

    {

        int num1=1, den1=500, num2=2, den2=1500, den3, num3;

        addFraction[num1, den1, num2, den2, num3, den3];

        printf["%d/%d + %d/%d is equal to %d/%d\n", num1, den1,

                                       num2, den2, num3, den3];

        return 0;

    }

    Java

    class GFG{

    static int gcd[int a, int b]

    {

        if [a == 0]

            return b;

        return gcd[b%a, a];

    }

    static void lowest[int den3, int num3]

    {

        int common_factor = gcd[num3,den3];

        den3 = den3/common_factor;

        num3 = num3/common_factor;

        System.out.println[num3+"/"+den3];

    }

    static void addFraction[int num1, int den1,

                            int num2, int den2]

    {

        int den3 = gcd[den1,den2];

        den3 = [den1*den2] / den3;

        int num3 = [num1]*[den3/den1] + [num2]*[den3/den2];

        lowest[den3,num3];

    }

    public static void main[String[] args]

    {

        int num1=1, den1=500, num2=2, den2=1500;

        System.out.print[num1+"/"+den1+" + "+num2+"/"+den2+" is equal to "];

        addFraction[num1, den1, num2, den2];

    }

    }

    Python3

    def gcd[a, b]:

        if [a == 0]:

            return b;

        return gcd[b % a, a];

    def lowest[den3, num3]:

        common_factor = gcd[num3, den3];

        den3 = int[den3 / common_factor];

        num3 = int[num3 / common_factor];

        print[num3, "/", den3];

    def addFraction[num1, den1, num2, den2]:

        den3 = gcd[den1, den2];

        den3 = [den1 * den2] / den3;

        num3 = [[num1] * [den3 / den1] +

                [num2] * [den3 / den2]];

        lowest[den3, num3];

    num1 = 1; den1 = 500;

    num2 = 2; den2 = 1500;

    print[num1, "/", den1, " + ", num2, "/",

          den2, " is equal to ", end = ""];

    addFraction[num1, den1, num2, den2];

    C#

    class GFG{

    static int gcd[int a, int b]

    {

        if [a == 0]

            return b;

        return gcd[b%a, a];

    }

    static void lowest[int den3, int num3]

    {

        int common_factor = gcd[num3,den3];

        den3 = den3/common_factor;

        num3 = num3/common_factor;

        System.Console.WriteLine[num3+"/"+den3];

    }

    static void addFraction[int num1, int den1, int num2, int den2]

    {

        int den3 = gcd[den1,den2];

        den3 = [den1*den2] / den3;

        int num3 = [num1]*[den3/den1] + [num2]*[den3/den2];

        lowest[den3,num3];

    }

    public static void Main[]

    {

        int num1=1, den1=500, num2=2, den2=1500;

        System.Console.Write[num1+"/"+den1+" + "+num2+"/"+den2+" is equal to "];

        addFraction[num1, den1, num2, den2];

    }

    }

    PHP

    Javascript

    const gcd = [a, b] => {

        if [a == 0]

            return b;

        return gcd[b % a, a];

    }

    const lowest = [den3, num3] => {

        let common_factor = gcd[num3, den3];

        den3 = parseInt[den3 / common_factor];

        num3 = parseInt[num3 / common_factor];

        document.write[`${num3}/${den3}`]

    }

    const addFraction = [num1, den1, num2, den2] => {

        let den3 = gcd[den1, den2];

        den3 = [den1 * den2] / den3;

        let num3 = [[num1] * [den3 / den1] +

                [num2] * [den3 / den2]];

        lowest[den3, num3];

    }

    let num1 = 1;

    let den1 = 500; 

    let num2 = 2;

    let den2 = 1500; 

    document.write[`${num1}/${den1} + ${num2}/${den2} is equal to `];

    addFraction[num1, den1, num2, den2];

    Output : 

    1/500 + 2/1500 is equal to 1/300

    Time Complexity: O[log[min[a, b]], where a and b are two integers.

    Auxiliary Space: O[1], no extra space required so it is a constant.

    See below for doing the same using library functions. 
    Ratio Manipulations in C++ | Set 1 [Arithmetic] 
    This article is contributed by Rahul Agrawal .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    Vote for difficulty

    Current difficulty : Easy

    How do you create a Fraction in Python?

    Fraction[string] : This requires the string or unicode instance and a fraction instance with same value is returned. Form for this instance : [sign] numerator ['/' denominator] Here, sign represents '+' or '-' and numerator and denominator are strings of single digits.

    How do you deal with fractions in Python?

    In Python the Fraction module supports rational number arithmetic. Using this module, we can create fractions from integers, floats, decimal and from some other numeric values and strings. There is a concept of Fraction Instance. It is formed by a pair of integers as numerator and denominator.

    How do you make a mixed Fraction in Python?

    Mixed Fractions in python.
    num = int[input['Type numerator']].
    dem = int[input['Type denominator']].
    a = num // dem..
    b = num % dem..
    print 'The mixed number is {} and {}/{}'. format[a, b, dem].

    Chủ Đề