Created
September 26, 2023 06:42
-
-
Save rebekah/c153c00fdb86d0c66f73c7231760ce9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyClass { | |
public static void main(String args[]) { | |
int numerator1 = 3; | |
int denominator1 = 5; | |
int numerator2 = 2; | |
int denominator2 = 15; | |
numerator1 = numerator1 * denominator2; | |
numerator2 = numerator2 * denominator1; | |
denominator1 = denominator1 * denominator2; | |
denominator2 = denominator2 * denominator1; | |
int newNumerator = numerator1 + numerator2; | |
System.out.println(String.format("the numerator1 is: %d", numerator1)); | |
System.out.println(String.format("the numerator2 is: %d", numerator2)); | |
System.out.println(String.format("the first newNumerator is: %d", newNumerator)); | |
int newDenominator = denominator1; | |
System.out.println(String.format("the first newDenominator is: %d", newDenominator)); | |
int gcd = gcdByEuclidsAlgorithm(newNumerator, newDenominator); | |
while(gcd != 1) { | |
newNumerator = newNumerator / gcd; | |
newDenominator = newDenominator / gcd; | |
gcd = gcdByEuclidsAlgorithm(newNumerator, newDenominator); | |
} | |
System.out.println(String.format("the new numerator is: %d and the new denominator is: %d", newNumerator, newDenominator)); | |
} | |
public static int gcdByEuclidsAlgorithm(int n1, int n2) { | |
if (n2 == 0) { | |
return n1; | |
} | |
return gcdByEuclidsAlgorithm(n2, n1 % n2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment