-1

So basically, I'm just trying to return a GCD from two previous numbers,

public static String GCD( int Denominator,int Numerator) {
    int newNum=Numerator;
    int newDen=Denominator;
    int newWhole=Whole;
    int GCD=0;
    if (Numerator == 0) {
        GCD = Denominator;
    }

    while (Denominator != 0) {
        if (Numerator > Denominator) {
            Numerator = Numerator - Denominator;
        }else {
            Denominator = Denominator - Numerator;
        }
    }
    GCD = Denominator;
    System.out.println(GCD);
}

You would think that since i declared GCD outside the loop that it would return just fine

but no, i just get 0 as an answer? almost every time..... thoughts?

PSo
  • 958
  • 9
  • 25
  • I suspect your GCD algorithm is off. For one thing, you never actually check if the steps you take result in a denominator which divides both numbers. Speaking of which, I don't even see two numerators in the input. – Tim Biegeleisen Dec 04 '17 at 06:30

2 Answers2

0

That's because your while loop is dependent on the value of Denominator as the loop ends when the value of Denominator is decreased to 0.

And right outside the while loop, you're storing the value of Denominator in 'GCD' which is obviously '0'.

So no matter what you do in the while loop, you would get 0 as the final result of your GCD.

I hope this solves your problem.

Safeer Ansari
  • 772
  • 4
  • 13
0

You need a return statement somewhere in your code. Probably at the end of the method, you need to write return GCD;.

Caders117
  • 319
  • 3
  • 18