Exercises 1


Prerequisite: GCD.

Write a function to simplify a fraction.

Tip: The function can be made to accept the numerator and denominator as reference parameters.

First - sketch your function on paper.
Next – implement a program to test it.
Greatest common denominator of two numbers a and b is calculated recursively by:

See Solution:

function GCD(a,b){ if(a === b){ return a; }else if(a>b){ return GCD(a-b,b); }else if(a<b){ return GCD(a,b-a); } } function Simplify(a,b){ var gcd = GCD(a,b); if(gcd===1){ return a+"/"+b; }else{ a /= gcd; b /= gcd; return Simplify(a,b); } } Fraction: a/b

Result :