Program to Compare Two Fractions in Java

In this Java application, we compare two fractions that the user submitted. It requests the numerators and denominators of the two fractions from the user, compares them, and outputs which is greater, less, or equal.

Example 1

Input:

Division 1: 4/5

Division 2: 2/3

Output: The Division 1 is more noteworthy than the Division 2

Explanation: We have two divisions in this illustration: 2/3 and 4/5. We to begin with distinguish a common denominator, which is 15, in arrange to compare them. Following, we alter the denominator of both divisions to the same: -

 Division 1: (12/15) = (4 * 3) / (5 * 3)

Division 2 is rise to to (2 * 5) / (3 * 5) = 10/15.

We presently differentiate the numerators: - Fraction 1 (12) numerator division> 2 (10) numerator Hence, the primary division (4/5) is bigger than the moment division (2/3) .

Types of Fractions 

  1. Fraction with Common Denominator
  2. Fractions with Different Denominator

Fraction with Common Denominator

Algorithm:

Step 1: Initialize the denominator1, numerator2, and denominator2 values.

Step 2: To use the compareFractionsWithCommonDenominator function, provide numerator1, denominator1, denominator2, and denominator2 as inputs.

Step 3: To get the least common multiple (LCM) of denominator 1 and denominator 2, use the findLCM method.

Step 4: To find the equivalent numerators for both fractions, use the method below: numerator * (LCM / denominator) is the comparable numerator.

Step 5: Examine analogous numerators

   In the event that equivalentNumerator1 > equivalentNumerator2, return "Fraction 1 is greater than Fraction 2".

   If equivalentNumerator 1 is less than equivalentNumerator 2, return "Fraction 2 is greater than Fraction 1".

   d. If equivalentNumerator 1 == equivalentNumerator 2, then return "Both fractions are equal".

Step 6: of the findLCM technique:

Take a look at a and b's product. b. Return the product divided by the greatest common divisor (GCD) of a and b.

Step 7: The method for finding GCD is as follows:

a. Calculate the GCD of a and b by applying Euclid's method.

b. Return the GCD.

FileName : FractionComparator.java

import java.util.*;   

public class FractionComparator {

    public static void main(String[] args) {

        int numerator1 = 3;

        int denominator1 = 4;

        int numerator2 = 1;

        int denominator2 = 2;

        String result = compareFractionsWithCommonDenominator(numerator1, denominator1, numerator2, denominator2);

        System.out.println(result);

    }

    public static String compareFractionsWithCommonDenominator(int numerator1, int denominator1, int numerator2, int denominator2) {

        int lcm = findLCM(denominator1, denominator2);

        int equivalentNumerator1 = numerator1 * (lcm / denominator1);

        int equivalentNumerator2 = numerator2 * (lcm / denominator2);

        if (equivalentNumerator1 > equivalentNumerator2) {

            return "Fraction 1 is greater than Fraction 2";

        } else if (equivalentNumerator1 < equivalentNumerator2) {

            return "Fraction 2 is greater than Fraction 1";

        } else {

            return "Both fractions are equal";

        }

    }

    // Least Common multiple

    public static int findLCM(int a, int b) {

        return (a * b) / findGCD(a, b);

    }

    public static int findGCD(int a, int b) {

        while (b != 0) {

            int temp = b;

            b = a % b;

            a = temp;

        }

        return a;

    }

}

Output

Fraction 1 is greater than Fraction 2

Complexity Analysis:

Time Complexity: O(log(min(denominator1, denominator2))) - Because the least common multiple (LCM) and greatest common divisor (GCD)

Space Complexity: O(1) – Constant Space

Fraction With Different Denominator

Algorithm

Step 1: Set the values of the two fractions for variables {numerator1}, {denominator1}, {numerator2}, and {denominator2}.

Step 2: Determine the products that {numerator1} and {denominator2} have with each other, and save the results as {product1} and {product2}, respectively.

Step 3: Compare {product1} and `product2}

Step 4: Return "Fraction 1 is greater than Fraction 2" if {product1} is greater than {product2}.

Step 5: Return "Fraction 2 is greater than Fraction 1" if {product1} is smaller than {product2}.

Step 6: Return "Both fractions are equal" if {product1} and {product2} are equal.

FileName: FractionComparator.java

public class FractionComparator {

    public static void main(String[] args) {

        int numerator1 = 3;

        int denominator1 = 4;

        int numerator2 = 1;

        int denominator2 = 2;

        String result = compareFractionsWithCrossMultiplication(numerator1, denominator1, numerator2, denominator2);

        System.out.println(result);

    }

    public static String compareFractionsWithCrossMultiplication(int numerator1, int denominator1, int numerator2, int denominator2) {

        int product1 = numerator1 * denominator2;

        int product2 = numerator2 * denominator1;

        if (product1 > product2) {

            return "Fraction 1 is greater than Fraction 2";

        } else if (product1 < product2) {

            return "Fraction 2 is greater than Fraction 1";

        } else {

            return "Both fractions are equal";

        }

    }

}

Output

Fraction 1 is greater than Fraction 2

Complexity Analysis:

Time Complexity: O(1) – Constant Time

Space Complexity: O(1) – Constant Space