Given two fractions a/b and c/d, compare them and print the larger of the two.
Input: 5/6, 11/45 Output: 5/6 Input: 4/5 and 2/3 Output: 4/5
We can simply convert the fractions in floating point values by dividing the numerator by the denominator. Once we have the two floating point numbers corresponding to each fraction, we can compare these numbers and determine which fraction is larger.
However the computed answer may be incorrect due to floating point approximations and truncations during the division process. To get the most accurate answer we should avoid resorting to floating point division.
To compare two fractions we need to make their denominators same. We can do this by cross-multiplying numerators with denominators. Lets see how this works
We have two fractions a/b and c/d. Let Y = (a/b - c/d) = (ad - bc)/(bd) Now if Y > 0 then a/b > c/d if Y = 0 then a/b = c/d if Y < o then a/b < c/d Since bd is always positive, the sign of Y depends only on the numerator (ad-bc). So we need to compute (ad-bc) only.
Since we perform two multiplication and one subtraction operation, the answer is computed in constant time i.e O(1)
- Program to add two fractions
- Program to compare m^n and n^m
- LCM and HCF of fractions
- Product of given N fractions in reduced form
- HCF of array of fractions (or rational numbers)
- Compare sum of first N-1 elements to Nth element of an array
- Compare numbers represented by Linked Lists
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to add two polynomials
- C program to calculate the value of nPr
- Program to calculate value of nCr
- Program for n-th even number
- Program for n-th odd number
- Program for sum of cos(x) series
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar