Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a calculator program and need to create GreatestCommononDivisor and ReduceFraction methods; but my GreatestCommonDivisor method isn't giving me the right answer, and I am not sure why.

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

C#
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;

   
    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;
}
}
Posted
Updated 7-Nov-14 15:06pm
v2

C#
public static int GreatestCommonDivisor(int a, int b)
{
    return b == 0 ? a : GreatestCommonDivisor(b, a % b);
}
 
Share this answer
 
Comments
PIEBALDconsult 7-Nov-14 21:34pm    
Hey, now, let's not go all recursive on this.
Link to algorithm for the benefit of others:
<href>http://en.wikipedia.org/wiki/Euclidean_algorithm[^]


I suspect you have a serious problem here:

C#
while (remainder == 0)
{

    if (remainder!= 0)
    {
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900