From the mind of Kon Pik…

Hopeless without the internet

Reducing Fractions

So for my current homework assignment, I had to do some pretty simple fraction reductions, ones that could be done in my head. Then, for some absurd reason I felt the urge to google around and see if i could find something that could reduce fractions FOR me. Of course I found many different tools. But… oddly enough they could only reduce fractions with a restrictions on how many digits could exist in the numerator and denominator. Most even had restrictions that the numerator and denominator had to be whole numbers (which is no fun if you’re randomly punching in numbers to see if it reduces). For more information on how why the code below works here’s some further reading:

Also, check out the fraction reducer that I wrote up in a quick few minutes.

[sourcecode]using System;
using System.Collections.Generic;
using System.Text;

namespace FReduce {
    class Program {

        private static double gcd(double a, double b) {
            return (b != 0 ? gcd(b, a % b) : a);

        static void Main(string[] args) {
            try {
                double num, den;
                Console.WriteLine("FReduce v1 by Kon \n\n");
                Console.Write("Enter the numerator:");
                num = Double.Parse(Console.ReadLine());
                Console.Write("\nEnter the denominator: ");
                den = Double.Parse(Console.ReadLine());
                solve(num, den);
            catch (Exception e) {
                Console.WriteLine("\n\n" + e.StackTrace.ToString());
                Console.WriteLine("\n\nSome shit went down as you see. Press Enter to exit.");

        private static void solve(double num, double den) {
            double g = gcd(num, den);
            Console.WriteLine("\nGCD of {0} and {1} is {2}", num, den, g);
            Console.WriteLine("{0}/{1} divided by {2}/{3} equals {4}/{5}",
                    num, den, g, g, num / g, den / g);
            Console.WriteLine("The reduced fraction is {0}/{1}", (num / g), (den / g));
            Console.WriteLine("\n\n\n\nPress Enter to exit");

Tags: , , , , , , ,