Last active
February 3, 2020 05:36
-
-
Save rekyuu/7aec20e2a2b7a893fa03be131f20299c 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
| using System; | |
| namespace sandbox | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Get the user input for both fractions. | |
| Console.WriteLine("Specify the first fraction."); | |
| Console.Write(" Numerator: "); | |
| int _pair1Top = Convert.ToInt32(Console.ReadLine()); | |
| Console.Write("Denominator: "); | |
| int _pair1Bot = Convert.ToInt32(Console.ReadLine()); | |
| Console.WriteLine("Specify the second fraction."); | |
| Console.Write(" Numerator: "); | |
| int _pair2Top = Convert.ToInt32(Console.ReadLine()); | |
| Console.Write("Denominator: "); | |
| int _pair2Bot = Convert.ToInt32(Console.ReadLine()); | |
| // Determine the raw result of the two fractions. | |
| int _resultTop = (_pair1Top * _pair2Bot) + (_pair2Top * _pair1Bot); | |
| int _resultBot = (_pair1Bot * _pair2Bot); | |
| Console.WriteLine($"Original result: {_resultTop} / {_resultBot}"); | |
| // Determine the greatest common denominator of the fraction. | |
| int _a = Math.Abs(_resultTop); | |
| int _b = Math.Abs(_resultBot); | |
| while (_a != _b) | |
| { | |
| if (_a < _b) _b -= _a; | |
| else _a -= _b; | |
| } | |
| int _divisor = _a; | |
| Console.WriteLine($"Greatest Common Denominator: {_divisor}"); | |
| // Reduce both the numerator and denominator by the divisor. | |
| int _reducedTop = _resultTop / _divisor; | |
| int _reducedBot = _resultBot / _divisor; | |
| // Return the result. | |
| Console.WriteLine($"Result: {_reducedTop} / {_reducedBot}"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment