0

I am creating a calculator program and need to create GreatestCommononDivisor and ReduceFraction methods; but my GreatestCommonDivisor method isn't getting the right value, and I am not sure why.

Can anyone provide any guidance or help in the right direction?

public static void ProcessReduceFraction()
{

    Int32 numerator, denominator, reducedNumerator, reducedDenominator; 

    numerator = GetPostiveNonZeroInteger("Please Enter Non-Zero Postive Numerator");
    denominator = GetPostiveNonZeroInteger("Please Enter Non-Zero Postive Denominator");
    reducedNumerator = numerator / GreatestCommonDivisor(denominator, numerator);
    reducedDenominator = denominator / GreatestCommonDivisor(numerator, denominator);

    Console.WriteLine("{0}/{1} can be reduced to: {2}/{3} ", numerator, denominator, reducedNumerator, reducedDenominator);
    Console.ReadLine();
}

public static Int32 GreatestCommonDivisor(Int32 r, Int32 n)
{

    Int32 remainder ,dividend, divisor, pervremainder;

    dividend = Math.Max(Math.Abs(n),Math.Abs(r));
    divisor = Math.Min(Math.Abs(n), Math.Abs(r));
    remainder = dividend % divisor;
    pervremainder = divisor;

    // Pull out remainders.
    while (remainder == 0)
    {

        if (remainder!= 0)
        {
            dividend = divisor;
            divisor = remainder;
            pervremainder = remainder;
            remainder = dividend % divisor;
        }
    }

    return pervremainder;
}

public static void ProcessGreatestCommonDivisor()
{

    Int32  firstnumber, secondnumber, greatestcommondivisor;

    firstnumber = GetPostiveNonZeroInteger("Plesae Enter Non-Zero Postive First Number");
    //firstnumber = Int32.Parse(Console.ReadLine());
    secondnumber = GetPostiveNonZeroInteger("Please Enter Non-Zero Postive Secound Number");
    //secoundnumber = Int32.Parse(Console.ReadLine());
    greatestcommondivisor = GreatestCommonDivisor(secondnumber, firstnumber);
    //greatestcommondivisor = Int32.Parse(Console.ReadLine());
    Console.WriteLine("The Greatest Common Divisor of {0} and {1} is: {2} ", 
    firstnumber,secondnumber, greatestcommondivisor);
    Console.ReadLine();
}

public static Int32 GetPostiveNonZeroInteger(String prompt)
{

    Int32 n;
    Console.WriteLine(prompt);
    n = Int32.Parse(Console.ReadLine());

    while (n <= 0)
    {
        Console.WriteLine("Error: enter non-zero postive value");
        Console.WriteLine(prompt);
        n = Int32.Parse(Console.ReadLine());
    }

    return n;
}

}

Tim
  • 28,212
  • 8
  • 63
  • 76
josh909
  • 13
  • 5
  • 3
    Is there any error? are the computations wrong? Is this a homework assignment? You might want to see this first: stackoverflow.com/help/how-to-ask – bit Nov 04 '14 at 06:26

1 Answers1

0

Since you mentioned about calculating Greatest Common Divisor.I think a simple approach would be

     public static int gcd(int dividend, int divisor)
    {
        while (divisor != 0)
        {
            int pervremainder = divisor;
            divisor = dividend % divisor;
            dividend = pervremainder;
        }
        return dividend;
    }

Alternatively you could also take a look at this Calculate GCD

Community
  • 1
  • 1
Rohit
  • 10,056
  • 7
  • 50
  • 82