Add two fractions | Practice | GeeksforGeeks

You are given four numbers num1, den1, num2, and den2. You need to find (num1/den1)+(num2/den2) and output the result in the form of (numx/denx). 
Input Format:
The first line of input contains an integer T denoting the number of test cases . Then T test cases follow . Each test case contains four integers num1, den1, num2, den2 respectively .
Output Format:
For each test case, in a new line,  output will be the fraction in the form a/b where the fraction denotes the sum of the two given fractions in reduced form.
Your Task:
Since this is a function problem, you don't need to worry about the testcases. Your task is to complete the function addFraction  which adds the two fractions and prints the resulting fraction. The function takes four arguments num1, den1, num2, den2 where num1, num2 denotes the numerators of two fractions and den1, den2 denotes their denominators.
Constraints:
1 <= T <= 100
1 <= den1,den2,num1,num2 <= 1000
Example:
Input
1
1 500 2 500
Output
3/500
Explanation:
In above test case 1/500+2/500=3/500
 
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
daxeshparmar02053 days ago
Without using GCD 
 int res1 = (num1 * den2) + (num2 * den1);       int res2 = den1 * den2;       int divider = 2;       int minOf2 = Math.min(res1,res2);       while (divider <= minOf2) {           if (res1 % divider == 0 && res2 % divider == 0){               res1 = res1 / divider;               res2 = res2 / divider;           } else {               divider++;           }       }       System.out.println(res1 + "/" + res2);
 
0
pranjalverma83492 weeks ago
// { Driver Code Starts#include<bits/stdc++.h>using namespace std;
void addFraction(int num1, int den1, int num2,                int den2);
int main(){   int T;   cin>>T;   while(T--)   {       int a,b,c,d,resultNum,resultDen;       cin>>a>>b>>c>>d;       addFraction(a,b,c,d);
   }}// } Driver Code Ends
long long gcd(long A,long B)   {                  if(B==0)       {           return A;                   }              return(gcd(B,A%B));   }void addFraction(int num1, int den1, int num2,int den2){       int l,g;          l=(den1*den2)/gcd(den1,den2);                    num1=num1*(l/den1)+num2*(l/den2);                    g=gcd(l,num1);                    cout<<num1/g<<"/"<<l/g<<endl;          }
+1
ayushshukla34663 months ago
void addFraction(int num1, int den1, int num2,int den2){//Your code here   long long den = (den1*den2)/__gcd(den1,den2);   long long n1 = den / den1;   long long n2 = den / den2;      long long num = (n1*num1)+(n2*num2);      long long n = __gcd(num,den);      num = num / n;   den = den / n;      cout << num << "/" << den << endl;   }
0
ramyan18143 months ago
int gcd(int a,int b){      if(b==0) return a;      return gcd(b,a%b);}
void addFraction(int num1, int den1, int num2,int den2){            int lcm=(den1*den2)/gcd(den1,den2);      int num=(lcm/den1)*num1+(lcm/den2)*num2;      int g=gcd(v,lcm);          v=v/g;          lcm=lcm/g;                 cout<<v<<"/"<<lcm<<"\n";        }
0
GURRAM VAMSI7 months ago
GURRAM VAMSI
def gcd(a,b):    if b==0:        return a    return gcd(b,a%b)def addFraction(n1, d1, n2, d2):    #Code here    num=(n1*d2+n2*d1)    dem=d1*d2    cf=gcd(num,dem)    print(num//cf,end='/')    print(dem//cf)
+1
Ritesh Keshri9 months ago
Ritesh Keshri
//EASIEST APPROACHint gcd(int a,int b){    if(a==0)    return b;
    return gcd(b%a,a);}
int lcm(int a,int b){    int n=gcd(a,b);
    return (n*(a/n)*(b/n));}void addFraction(int num1, int den1, int num2,int den2){   int n=lcm(den1,den2);   int sum=(n/den1)*num1 + (n/den2)*num2;
   int m=gcd(n,sum);    n=n/m; sum=sum/m;   cout<<sum<<" "<<n;="" }="">
0
sankirtana gv9 months ago
sankirtana gv
void addFraction(int num1, int den1, int num2,int den2){    int lcm = (den1*den2)/__gcd(den1,den2);    int v = (lcm/den1)*num1 + (lcm/den2)*num2;    int g = __gcd(v,lcm);    v = v/g;    lcm = lcm/g;    cout<<v<<" "<<lcm<<"\n";="" }="">
0
Rajesh Kumar9 months ago
Rajesh Kumar
long long int gcd(long long int den1,long long int den2){    if(den1==0)        return den2;    return gcd(den2%den1,den1);}
long long int lcmoftwo(long long int den1,long long int den2){    long long int hcf=gcd(den1,den2);    return (den1*den2)/hcf;}//You are required to complete this function/void addFraction(int num1, int den1, int num2,int den2){    long long int sum=0;    if(num1==0)        cout<<num2<<" "<<den2<<endl;="" if(num2="=0)" cout<<num1<<"="" "<<den1<<endl;="" if(den1="=den2)" {;="" cout<<num1+num2<<"="" "<<den1<<endl;="" }="" else="" {="" long="" long="" int="" lcm="lcmoftwo(den1,den2);" long="" long="" int="" rem1="lcm/den1;" long="" long="" int="" rem2="lcm/den2;" sum="(num1*rem1+num2*rem2);" cout<<sum<<"="" "<<lcm<<endl;="" }="" }how="" can="" i="" do="" fraction="" in="" simple="">
0
Sawi Sharma1 year ago
Sawi Sharma
Python3 solusann:
def addFraction(num1, den1, num2, den2):    #Code here    import math    nums=num1*den2+num2*den1    dens=den1*den2    gcd=math.gcd(nums,dens)    print(str(int(nums/gcd))+"/"+str(int(dens/gcd)))
0
Mahesh Mehetre1 year ago
Mahesh Mehetre
Execution Time:0.01https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.