Add two fractions

PROBLEM :

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.

Input:
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:
For each test case output will be the fraction in the form a/b where the fraction denotes the sum of the two given fractions in reduced form.

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
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*You are required to complete this function*/

int findGCD(int ,int ) ;

void addFraction(int num1, int den1, int num2,int den2)
{
    int den3,num3,comm ;
   
    den3=den1>den2?findGCD(den2,den1):findGCD(den1,den2) ;
   
    den3=(den1*den2)/den3 ;
   
    num3=num1*(den3/den1)+num2*(den3/den2) ;
   
    comm=num3>den3?findGCD(den3,num3):findGCD(num3,den3) ;
   
    num3=num3/comm ;
    den3=den3/comm ;
   
    cout<<num3<<"/"<<den3<<endl ;
}

int findGCD(int no1,int no2)
{
    int rem ;
    rem=no2%no1 ;
    if(rem==0)
        return no1 ;
    else
        return findGCD(rem,no1) ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )