Given two fractions with some numerator nume1 and nume2 and deno1 and deno2 as their respective denominator, the task is to compare both the fractions and find out the greater one. Like we have a fraction 1/2 and 2/3 and the higher one is 2/3 because the value of 1/2 is 0.5 and the value of 2/3 is 0.66667 which is higher.
Input
first.nume = 2, first.deno = 3 second.nume = 4, second.deno = 3
Output
4/3
Explanation
2/3 = 0.66667 < 4/3 = 1.33333
Input
first.nume = 1, first.deno = 2 second.nume = 4, second.deno = 3
Output
4/3
//baadme likhunga
Start Declare a struct Fraction with elements nume, deno In function Fraction greater(Fraction first, Fraction sec) Step 1→ Declare and Initialize t Y = first.nume * sec.deno - first.deno * sec.nume Step 2→ Return (Y > 0) ? first : sec In function int main() Step 1→ Declare Fraction first = { 4, 5 } Step 2→Fraction sec = { 3, 4 } Step 3→ Fraction res = greater(first, sec) Step 4→ Print res.nume, res.deno Stop
#include <stdio.h> struct Fraction { int nume, deno; }; // Get max of the two fractions Fraction greater(Fraction first, Fraction sec){ //check if the result is in negative then the //second fraction is greater else first is greater int Y = first.nume * sec.deno - first.deno * sec.nume; return (Y > 0) ? first : sec; } int main(){ Fraction first = { 4, 5 }; Fraction sec = { 3, 4 }; Fraction res = greater(first, sec); printf("The greater fraction is: %d/%d\n", res.nume, res.deno); return 0; }
If run the above code it will generate the following output −
The greater fraction is: 4/5