Program to compare two fractions
Last Updated :
06 Aug, 2024
Given two fractions a/b and c/d, compare them and print the larger of the two.
Examples :
Input: 5/6, 11/45
Output: 5/6
Input: 4/5 and 2/3
Output: 4/5
We can simply convert the fractions into 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 the same. We can do this by cross-multiplying numerators with denominators. Let's 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.
Complexity :
Since we perform two multiplication and one subtraction operation, the answer is computed in constant time i.e O(1)
C++
// CPP program to find max between
// two Rational numbers
#include <bits/stdc++.h>
using namespace std;
struct Fraction {
int num, den;
};
// Get max of the two fractions
Fraction maxFraction(Fraction first, Fraction sec)
{
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
// Compute ad-bc
int Y = a * d - b * c;
return (Y > 0) ? first : sec;
}
// Driver Code
int main()
{
Fraction first = { 3, 2 };
Fraction sec = { 3, 4 };
Fraction res = maxFraction(first, sec);
cout << res.num << "/" << res.den;
return 0;
}
Java
// Java program to find max between
// two Rational numbers
import java.io.*;
import java.util.*;
class Fraction {
int num, den;
//Constructor
Fraction(int n,int d){
num=n;
den=d;
}
// Get max of the two fractions
static Fraction maxFraction(Fraction first, Fraction sec)
{
// Declare nume1 and nume2 for get the value of
// first numerator and second numerator
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
// Compute ad-bc
int Y = a * d - b * c;
return (Y > 0) ? first : sec;
}
// Driver function
public static void main (String[] args) {
Fraction first = new Fraction( 3, 2 );
Fraction sec = new Fraction( 3, 4 );
Fraction res = maxFraction(first, sec);
System.out.println(res.num +"/"+ res.den);
}
}
// This code is contributed by Gitanjali.
Python3
# Python3 program to find max
# between two Rational numbers
# Get max of the two fractions
def maxFraction(first, sec):
# Declare nume1 and nume2 for get the value
# of first numerator and second numerator
a = first[0]; b = first[1]
c = sec[0]; d = sec[1]
# Compute ad-bc
Y = a * d - b * c
return first if Y else sec
# Driver Code
first = ( 3, 2 )
sec = ( 3, 4 )
res = maxFraction(first, sec)
print(str(res[0]) + "/" + str(res[1]))
# This code is contributed by Ansu Kumari.
C#
// C# program to find max between
// two Rational numbers
using System;
class Fraction {
int num, den;
//Constructor
Fraction(int n,int d)
{
num=n;
den=d;
}
// Get max of the two fractions
static Fraction maxFraction(Fraction first, Fraction sec)
{
// Declare nume1 and nume2 for get the value of
// first numerator and second numerator
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
// Compute ad-bc
int Y = a * d - b * c;
return (Y > 0) ? first : sec;
}
// Driver function
public static void Main ()
{
Fraction first = new Fraction( 3, 2 );
Fraction sec = new Fraction( 3, 4 );
Fraction res = maxFraction(first, sec);
Console.WriteLine(res.num +"/"+ res.den);
}
}
// This code is contributed by vt_m.
JavaScript
// javascript program to find max
// between two Rational numbers
// Get max of the two fractions
function maxFraction(first, sec) {
// Declare nume1 and nume2 for get the value
// of first numerator and second numerator
a = first[0]; b = first[1]
c = sec[0]; d = sec[1]
// Compute ad-bc
Y = a * d - b * c
return (Y > 0) ? first : sec;
}
// Driver Code
first = [ 3, 2 ];
sec = [ 3, 4 ];
res = maxFraction(first, sec);
document.write(res[0] + "/" + res[1]);
PHP
<?php
// PHP program to find max
// between two Rational numbers
// Get max of the two fractions
function maxFraction($first, $sec)
{
// Declare nume1 and nume2
// for get the value of
// first numerator and second
// numerator
$a = $first[0];
$b = $first[1];
$c = $sec[0];
$d = $sec[1];
// Compute ad-bc
$Y = $a * $d - $b * $c;
return ($Y) ? $first : $sec;
}
// Driver Code
$first = array( 3, 2 );
$sec = array ( 3, 4 );
$res = maxFraction($first, $sec);
echo $res[0] . "/" . $res[1];
// This code is contributed
// by mits.
?>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Program to add two fractions Given two integer arrays a[] and b[] containing two integers each representing the numerator and denominator of a fraction respectively. The task is to find the sum of the two fractions and return the numerator and denominator of the result. Examples : Input: a = [1, 2] , b = [3, 2] Output: [2, 1] E
6 min read
Fraction to Recurring Decimal Given two integers a and b(b != 0), the task is to return the fraction a/b in string format. If the fractional part is repeating, enclose the repeating part in parentheses.Examples: Input: a = 1, b = 2Output: "0.5"Explanation: 1/2 = 0.5 with no repeating part.Input: a = 50, b = 22Output: "2.(27)"Exp
8 min read
Compare given two powers of 10 Given 4 integers A, B, Z1, and Z2. The task is to compare A*10Z1 and B*10Z2. Examples: Input: A = 19, Z1 = 2, B = 20, Z2 = 1Output: A > BExplanation:A can be written as 1900 B can be written as 200 So, A is greater than B. Input:, A = 199, Z1 =10, B = 96, Z2 = 1000Output: A < BExplanation:A ca
7 min read
Fractions Practice Questions (Easy) Fractions are a way of representing parts of a whole. They express a number in terms of a numerator and a denominator, where:Numerator: The top number, represents how many parts we are considering.Denominator: The bottom number, represents the total number of equal parts that make up the whole.Read
3 min read
Reduce the fraction to its lowest form Given two integers x and y and where x is divisible by y. It can be represented in the form of a fraction x/y. The task is to reduce the fraction to its lowest form.Examples: Input : x = 16, y = 10 Output : x = 8, y = 5 Input : x = 10, y = 8 Output : x = 5, y = 4 Approach: Both of the values x and y
4 min read