none
Reducing a Fraction (URGENT!!!) RRS feed

  • Question

  • Hello, I am trying to do a project for my programming class and the only thing i am having issues with is trying to reduce fractions to their simplest form. Here are the two classes:

    using System;
    public static class Driver
    {
        public static void Main()
        {
            char response = 'Y';
            do
            {
                int num1, num2;
                Fraction fraction1 = new Fraction();
                Fraction fraction3;
    
                Console.Write("Enter a numerator for the first fraction: ");
                fraction1.Numerator = (Convert.ToInt32(Console.ReadLine()));
                Console.Write("Enter a denominator for the first fraction: ");
                fraction1.Denominator = (Convert.ToInt32(Console.ReadLine()));
                Console.Write("Enter a numerator for the second fraction: ");
                num1 = (Convert.ToInt32(Console.ReadLine()));
                Console.Write("Enter a denominator for the second fraction: ");
                num2 = (Convert.ToInt32(Console.ReadLine()));
    
                Fraction fraction2 = new Fraction(num1, num2);
    
                Console.WriteLine("The first fraction: {0}", fraction1.ToString());
                Console.WriteLine("The second fraction: {0}", fraction2.ToString());
    
                fraction3 = fraction1 * fraction2;
                Console.WriteLine("{0} x {1} = {2}", fraction1.ToString(), fraction2.ToString(), fraction3.ToString());
                fraction3 = fraction1 + fraction2;
                Console.WriteLine("{0} + {1} = {2}", fraction1.ToString(), fraction2.ToString(), fraction3.ToString());
    
                if (fraction1 >= fraction2)
                {
                    Console.WriteLine("{0} IS greater than or equal to {1}", fraction1.ToString(), fraction2.ToString());
                }
                else
                {
                    Console.WriteLine("{0} is NOT greater than or equal to {1}", fraction1.ToString(), fraction2.ToString());
                }
                if (fraction1 <= fraction2)
                {
                    Console.WriteLine("{0} IS less than or equal to {1}", fraction1.ToString(), fraction2.ToString());
                }
                else
                {
                    Console.WriteLine("{0} is NOT less than or equal to {1}", fraction1.ToString(), fraction2.ToString());
                }
    
                do
                {
                    Console.WriteLine("Do you want to restart? Type 'Y' for yes or 'N' for no.");
                    response = (Convert.ToChar(Console.ReadLine()));
                } while (response != 'N' && response != 'n' && response != 'Y' && response != 'y');
    
            } while (response != 'N' && response != 'n');
            Console.ReadLine();
        }
    }

    using System;
    public class Fraction
    {
        private int numerator;
        private int denominator;
    
        // no parameter constructor.
        public Fraction()
        {
            numerator = 0;
            denominator = 1;
        }
    
        // two parameter constructor.
        public Fraction(int num, int den)
        {
            numerator = num;
            denominator = den;
            Reduce();
        }
    
        // numerator property.
        public int Numerator
        {
            get
            { return numerator; }
            set
            { numerator = value; }
        }
    
        // denominator property.
        public int Denominator
        {
             get
            { return denominator; }
            set
            {
                if (denominator == 0)
                    denominator = 1;
                else
                    denominator = value;
            }
            
        }
    
        // reduce method.
        private void Reduce()
        {
            int GCD = 0;
            for (int x = 1; x >= denominator; x++)
            {
                if ((numerator % x == 0) && (denominator % x == 0))
                    GCD = x;
            }
            if (GCD != 0)
            {
                numerator = numerator / GCD;
                denominator = denominator / GCD;
            }
        }
    
        // to string method.
        public override string ToString()
        {
            return numerator + "/" + denominator;
        }
    
        // multiply method.
        public static Fraction operator*(Fraction fract1, Fraction fract2)
        {
            Fraction multipication = new Fraction();
            multipication.numerator = fract1.numerator * fract2.numerator;
            multipication.denominator = fract1.denominator * fract2.denominator;
            multipication.Reduce();
            return multipication;
        }
    
        // add operator.
        public static Fraction operator+(Fraction fract1, Fraction fract2)
        {
            Fraction added = new Fraction();
            int newnum1, newnum2;
            newnum1 = fract1.numerator * fract2.denominator;
            newnum2 = fract2.numerator * fract1.denominator;
            added.numerator = newnum1 + newnum2;
            added.denominator = fract1.denominator * fract2.denominator;
            added.Reduce();
            return added;
        }
    
        // greater than or equal operator.
        public static bool operator>=(Fraction fract1, Fraction fract2)
        {
            double val1 = (double)fract1.numerator / fract1.denominator;
            double val2 = (double)fract2.numerator / fract2.denominator;
            if (val1 >= val2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        // less than or equal operator.
        public static bool operator<=(Fraction fract1, Fraction fract2)
        {
            double val1 = (double)fract1.numerator / fract1.denominator;
            double val2 = (double)fract2.numerator / fract2.denominator;
            if (val1 <= val2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
    }

    (P.S. I am not allowed to change the headers of or add any additional methods, properties, constructors, etc. to the Fraction class).

    Friday, April 6, 2018 4:53 PM

Answers

  • May This Help You

        private void Reduce()
        {
            int GCD = 0;
            for (int x = 1; x <= denominator; x++)
            {
                if ((numerator % x == 0) && (denominator % x == 0))
                    GCD = x;
            }
            if (GCD != 0)
            {
                numerator = numerator / GCD;
                denominator = denominator / GCD;
            }
    
        }

    • Marked as answer by blackbearox Friday, April 6, 2018 5:21 PM
    Friday, April 6, 2018 5:09 PM

All replies

  • May This Help You

        private void Reduce()
        {
            int GCD = 0;
            for (int x = 1; x <= denominator; x++)
            {
                if ((numerator % x == 0) && (denominator % x == 0))
                    GCD = x;
            }
            if (GCD != 0)
            {
                numerator = numerator / GCD;
                denominator = denominator / GCD;
            }
    
        }

    • Marked as answer by blackbearox Friday, April 6, 2018 5:21 PM
    Friday, April 6, 2018 5:09 PM
  • Thank you! I make dumb little mistakes like that way too much haha.
    Friday, April 6, 2018 5:22 PM