# Program to add two fractions

• Difficulty Level : Easy
• Last Updated : 23 May, 2022

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```

Recommended Practice

• 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 there largest common factor.

## C++

 `// C++ program to add 2 fractions``#include``using` `namespace` `std;` `// Function to return gcd of a and b``int` `gcd(``int` `a, ``int` `b)``{``    ``if` `(a == 0)``        ``return` `b;``    ``return` `gcd(b%a, a);``}` `// Function to convert the obtained fraction``// into it's simplest form``void` `lowest(``int` `&den3, ``int` `&num3)``{``    ``// Finding gcd of both terms``    ``int` `common_factor = gcd(num3,den3);` `    ``// Converting both terms into simpler``    ``// terms by dividing them by common factor``    ``den3 = den3/common_factor;``    ``num3 = num3/common_factor;``}` `//Function to add two fractions``void` `addFraction(``int` `num1, ``int` `den1, ``int` `num2,``                 ``int` `den2, ``int` `&num3, ``int` `&den3)``{``    ``// Finding gcd of den1 and den2``    ``den3 = gcd(den1,den2);` `    ``// Denominator of final fraction obtained``    ``// finding LCM of den1 and den2``    ``// LCM * GCD = a * b``    ``den3 = (den1*den2) / den3;` `    ``// Changing the fractions to have same denominator``    ``// Numerator of the final fraction obtained``    ``num3 = (num1)*(den3/den1) + (num2)*(den3/den2);` `    ``// Calling function to convert final fraction``    ``// into it's simplest form``    ``lowest(den3,num3);``}` `// Driver program``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

 `// Java program to add 2 fractions` `class` `GFG{``// Function to return gcd of a and b``static` `int` `gcd(``int` `a, ``int` `b)``{``    ``if` `(a == ``0``)``        ``return` `b;``    ``return` `gcd(b%a, a);``}` `// Function to convert the obtained fraction``// into it's simplest form``static` `void` `lowest(``int` `den3, ``int` `num3)``{``    ``// Finding gcd of both terms``    ``int` `common_factor = gcd(num3,den3);` `    ``// Converting both terms into simpler``    ``// terms by dividing them by common factor``    ``den3 = den3/common_factor;``    ``num3 = num3/common_factor;``    ``System.out.println(num3+``"/"``+den3);``}` `//Function to add two fractions``static` `void` `addFraction(``int` `num1, ``int` `den1,``                        ``int` `num2, ``int` `den2)``{``    ``// Finding gcd of den1 and den2``    ``int` `den3 = gcd(den1,den2);` `    ``// Denominator of final fraction obtained``    ``// finding LCM of den1 and den2``    ``// LCM * GCD = a * b``    ``den3 = (den1*den2) / den3;` `    ``// Changing the fractions to have same denominator``    ``// Numerator of the final fraction obtained``    ``int` `num3 = (num1)*(den3/den1) + (num2)*(den3/den2);` `    ``// Calling function to convert final fraction``    ``// into it's simplest form``    ``lowest(den3,num3);``}` `// Driver program``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);``}``}``// This code is contributed by mits`

## Python3

 `# Python3 program to add 2 fractions` `# Function to return gcd of a and b``def` `gcd(a, b):``    ``if` `(a ``=``=` `0``):``        ``return` `b;``    ``return` `gcd(b ``%` `a, a);` `# Function to convert the obtained``# fraction into it's simplest form``def` `lowest(den3, num3):` `    ``# Finding gcd of both terms``    ``common_factor ``=` `gcd(num3, den3);` `    ``# Converting both terms``    ``# into simpler terms by``    ``# dividing them by common factor``    ``den3 ``=` `int``(den3 ``/` `common_factor);``    ``num3 ``=` `int``(num3 ``/` `common_factor);``    ``print``(num3, ``"/"``, den3);` `# Function to add two fractions``def` `addFraction(num1, den1, num2, den2):` `    ``# Finding gcd of den1 and den2``    ``den3 ``=` `gcd(den1, den2);` `    ``# Denominator of final``    ``# fraction obtained finding``    ``# LCM of den1 and den2``    ``# LCM * GCD = a * b``    ``den3 ``=` `(den1 ``*` `den2) ``/` `den3;` `    ``# Changing the fractions to``    ``# have same denominator Numerator``    ``# of the final fraction obtained``    ``num3 ``=` `((num1) ``*` `(den3 ``/` `den1) ``+``            ``(num2) ``*` `(den3 ``/` `den2));` `    ``# Calling function to convert``    ``# final fraction into it's``    ``# simplest form``    ``lowest(den3, num3);` `# Driver Code``num1 ``=` `1``; den1 ``=` `500``;``num2 ``=` `2``; den2 ``=` `1500``;` `print``(num1, ``"/"``, den1, ``" + "``, num2, ``"/"``,``      ``den2, ``" is equal to "``, end ``=` `"");``addFraction(num1, den1, num2, den2);` `# This code is contributed by mits`

## C#

 `// C# program to add 2 fractions` `class` `GFG{``// Function to return gcd of a and b``static` `int` `gcd(``int` `a, ``int` `b)``{``    ``if` `(a == 0)``        ``return` `b;``    ``return` `gcd(b%a, a);``}` `// Function to convert the obtained fraction``// into it's simplest form``static` `void` `lowest(``int` `den3, ``int` `num3)``{``    ``// Finding gcd of both terms``    ``int` `common_factor = gcd(num3,den3);` `    ``// Converting both terms into simpler``    ``// terms by dividing them by common factor``    ``den3 = den3/common_factor;``    ``num3 = num3/common_factor;``    ``System.Console.WriteLine(num3+``"/"``+den3);``}` `//Function to add two fractions``static` `void` `addFraction(``int` `num1, ``int` `den1, ``int` `num2, ``int` `den2)``{``    ``// Finding gcd of den1 and den2``    ``int` `den3 = gcd(den1,den2);` `    ``// Denominator of final fraction obtained``    ``// finding LCM of den1 and den2``    ``// LCM * GCD = a * b``    ``den3 = (den1*den2) / den3;` `    ``// Changing the fractions to have same denominator``    ``// Numerator of the final fraction obtained``    ``int` `num3 = (num1)*(den3/den1) + (num2)*(den3/den2);` `    ``// Calling function to convert final fraction``    ``// into it's simplest form``    ``lowest(den3,num3);``}` `// Driver program``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);``}``}``// This code is contributed by mits`

## PHP

 ``

## Javascript

 ``

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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
{"ft_lang_label":"__label__en","ft_lang_prob":0.712625,"math_prob":0.9819918,"size":8940,"snap":"2022-27-2022-33","text_gpt3_token_len":2932,"char_repetition_ratio":0.1558863,"word_repetition_ratio":0.4662614,"special_character_ratio":0.35816556,"punctuation_ratio":0.15507692,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99974126,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-15T09:41:31Z\",\"WARC-Record-ID\":\"<urn:uuid:83d26a3c-9d47-4541-ae03-b768e674b5e4>\",\"Content-Length\":\"183959\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:963d624b-b111-4aff-9884-b7d4437e27c1>\",\"WARC-Concurrent-To\":\"<urn:uuid:7b5bfc7a-c746-4bbf-98f6-7bb94f806d8e>\",\"WARC-IP-Address\":\"23.62.230.194\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/program-to-add-two-fractions/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:XV7V7MOYREL5HOYTN57IVGBQJITY7LE3\",\"WARC-Block-Digest\":\"sha1:AJDDYQP7CN6XOC6DJL6FIKUPN3TO7AGB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882572163.61_warc_CC-MAIN-20220815085006-20220815115006-00190.warc.gz\"}"}