Hi Everyone..
Following is the code snippet i wrote as an one of my assignment in programming course…It simplifies the fraction to lowest level though code is not optimized. It needs improvement…
void cancel_fraction (long &upper, long &bottom){int i,j,k,limit;
if (upper<bottom) // Checking for Small number and placing it in limit
limit=upper;
else
limit=bottom;
for (i=2; i<=limit; i++) // (LOOP 1) Main Table Starting From 2 to small Fraction
{
for (j=1; j<=upper; j++) // (LOOP 2) Table Index 1 to num1
{
if (i*j==upper) // (Condition 1) num1 is factor of current table ‘i’ then check for sencond fraction i.e., num2
{
for (k=1; k<=bottom; k++) // (LOOP 3) table index for second fraction
{
if (i*k==bottom) // (Condition 2) num2 is factor of current table ‘i’ then
{
bottom=k; // num2 is divided by table ‘i’ at ‘k’
upper=j; // num1 is also divided by table ‘i’ but at ‘j’ thus
i–; // to divide the fraction again by the same table
} // End of (Condition 2)
} // End of (LOOP 3)
} // End of (Condition 1)
} // End of (LOOP 2)
} // End of (LOOP 1)
}
–
Keep Smiling