Find max of two Rational numbers

Given two Rational numbers, the task is to find the maximum of given two rational numbers.

Examples :

Input : first = 3/4, second= 3/2
Output : 3/2

Input : first = 100/100, second = 300/400
Output : 100/100



A simple solution is to find float values and compare the float values. The float computations may cause precision errors. We can avoid them using below approach.

Say first = 3/2, second = 3/4

  • First take a LCM of (4, 2) which is denominator of rational number. so the LCM of this is 4, then divide with both denominator and multiple with numerator of first and second respectively so the denominator value is first numerator = 6, second numerator = 3.
  • Then find the max between these two. so here first numerator is max then print first rational number.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find max between
// two Rational numbers
#include <bits/stdc++.h>
using namespace std;
  
struct Rational
{
    int nume, deno;
};
  
// Get lcm of two number's
int lcm(int a, int b) 
{
    return (a * b) / (__gcd(a, b));
}
  
// Get max rational number
Rational maxRational(Rational first, Rational sec)
{
    // Find the LCM of first->denominator
    // and sec->denominator
    int k = lcm(first.deno, sec.deno);
  
    // Declare nume1 and nume2 for get the value of
    // first numerator and second numerator
    int nume1 = first.nume;
    int nume2 = sec.nume;
  
    nume1 *= k / (first.deno);
    nume2 *= k / (sec.deno);
  
    return (nume2 < nume1)? first : sec;
}
  
// Driver Code
int main()
{
    Rational first = { 3, 2 };
    Rational sec = { 3, 4 };
  
    Rational res = maxRational(first, sec);
    cout << res.nume << "/" << res.deno;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to find max between
// two Rational numbers
  
class GFG
{
  
static class Rational
{
    int nume, deno;
  
    public Rational(int nume, int deno) 
    {
        this.nume = nume;
        this.deno = deno;
    }
      
};
  
// Get lcm of two number's
static int lcm(int a, int b) 
{
    return (a * b) / (__gcd(a, b));
}
  
// Get max rational number
static Rational maxRational(Rational first, Rational sec)
{
    // Find the LCM of first.denominator
    // and sec.denominator
    int k = lcm(first.deno, sec.deno);
  
    // Declare nume1 and nume2 for get the value of
    // first numerator and second numerator
    int nume1 = first.nume;
    int nume2 = sec.nume;
  
    nume1 *= k / (first.deno);
    nume2 *= k / (sec.deno);
  
    return (nume2 < nume1)? first : sec;
}
static int __gcd(int a, int b) 
    return b == 0 ? a:__gcd(b, a % b);     
}
  
// Driver Code
public static void main(String[] args)
{
    Rational first = new Rational(3, 2 );
    Rational sec = new Rational(3, 4 );
  
    Rational res = maxRational(first, sec);
    System.out.print(res.nume+ "/" + res.deno);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output :

3/2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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 : Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.