- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find max of two Rational numbers in C++

In this problem, we are given two *Rational Numbers*. Our task is to *find max of two Rational numbers. *

Here, the rational numbers are in the form of p/q.

**Let’s take an example to understand the problem, **

**Input: **rat1 = 5/4, rat2 = 3/2

**Output: **3/2

**Explanation: **

5/4 = 1.25

3/2 = 1.5

## Solution Approach −

A simple solution to the problem is by using a method similar to the one we used to perform in school.

For this, we will find the *L.C.M* of the denominator. And then multiply the numerator based on the denominators value. Then for the common denominator, the rational number with maximum numerator value is the max one.

## Program to illustrate the working of our solution,

## Example

#include <bits/stdc++.h> using namespace std; int findLCM(int a, int b) { return (a * b) / (__gcd(a, b)); } void maxRational(int ratOneNum, int ratOneDen, int ratTwoNum, int ratTwoDen) { int k = findLCM(ratOneDen, ratTwoDen); int oneNum = ratOneNum * k / (ratOneDen); int twoNum = ratTwoNum * k / (ratTwoDen); if(oneNum > twoNum) cout<<ratOneNum<<"/"<<ratOneDen; else cout<<ratTwoNum<<"/"<<ratTwoDen; } int main() { int ratOneNum = 5; int ratOneDen = 4; int ratTwoNum = 3; int ratTwoDen = 2; cout<<"The maximum of the two rational Numbers is "; maxRational(ratOneNum, ratOneDen, ratTwoNum, ratTwoDen); return 0; }

## Output

The maximum of the two rational Numbers is 3/2

- Related Articles
- Find two rational numbers between $1$ and $2$.
- How do we find the product of two rational numbers?
- How many rational numbers are there between two rational numbers?
- Find two rational numbers between $-4$ and $frac{5}{4}$.
- Equal Rational Numbers in C++
- Find three rational numbers between:
- The product of two rational numbers is 15. If one of the numbers is $-10$, find the other.
- Find two rational numbers between $frac{1}{5}$ and $frac{1}{2}$.
- Average of max K numbers in a stream in C++
- The sum of two rational numbers is $-8$. If one of the numbers is $−frac{15}{7}$ find the other.
- The product of two rational numbers is $frac{-8}{9}$ if one of the numbers is $frac{-4}{15}$, find other.
- What are rational numbers? Types of rational number.
- Find LCM of rational number in C++
- Given two rational numbers lying between $0.232332333233332…$ and $0.212112111211112......$.
- Give two rational numbers lying between $0.515115111511115…$ and $0.5353353335…$

Advertisements