Program to add two fractions - GeeksforGeeks

22 Mar, 2021
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 there largest common factor.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to add 2 fractions#include<bits/stdc++.h>using namespace std; // Function to return gcd of a and bint 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 formvoid 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 fractionsvoid 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 programint 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 program to add 2 fractions class GFG{// Function to return gcd of a and bstatic 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 formstatic 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 fractionsstatic 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 programpublic 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 program to add 2 fractions # Function to return gcd of a and bdef gcd(a, b):    if (a == 0):        return b;    return gcd(b % a, a); # Function to convert the obtained# fraction into it's simplest formdef 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 fractionsdef 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 Codenum1 = 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# program to add 2 fractions class GFG{// Function to return gcd of a and bstatic 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 formstatic 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 fractionsstatic 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 programpublic 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// PHP program to add// 2 fractions // Function to return// gcd of a and bfunction gcd($a, $b){    if ($a == 0)        return $b;    return gcd($b % $a, $a);} // Function to convert the// obtained fraction into// it's simplest formfunction 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;} // Function to add// two fractionsfunction addFraction($num1, $den1, $num2,                     $den2, &$num3, &$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 Code$num1 = 1; $den1 = 500;$num2 = 2; $den2 = 1500;$den3; $num3;addFraction($num1, $den1, $num2,            $den2, $num3, $den3);echo $num1, "/", $den1, " + ",     $num2,"/", $den2, " is equal to ",               $num3, "/", $den3, "\n";             // This code is contributed by aj_36?>
<script> // Javascript program to add 2 fractions   // Function to return gcd of a and b const gcd = (a, b) => {    if (a == 0)        return b;    return gcd(b % a, a);} // Function to convert the // obtained fraction into // it's simplest form const lowest = (den3, num3) => {    // Finding gcd of both terms    let common_factor = gcd(num3, den3);       // Converting both terms      // into simpler terms by     // dividing them by common factor            den3 = parseInt(den3 / common_factor);    num3 = parseInt(num3 / common_factor);       document.write(`${num3}/${den3}`)}  // Function to add two fractionsconst addFraction = (num1, den1, num2, den2) => {    // Finding gcd of den1 and den2    let 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    let num3 = ((num1) * (den3 / den1) +            (num2) * (den3 / den2));       // Calling function to convert     // final fraction into it's     // simplest form    lowest(den3, num3);} // Driver Codelet 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);               // This code is contributed by _saurabh_jaiswal </script>
Output : 
1/500 + 2/1500 is equal to 1/300
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Mithun Kumar
_saurabh_jaiswal
Fraction
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value
Program for Decimal to Binary Conversion
Program to find sum of elements in a given array