samedi 27 juillet 2019

Euclid's algorithm shows wrong conclusion

Homework of mine is to create and Euclid's algorithm in java. The task binds me to use both while-loop and if statement. Futhermore - if statement has to be placed inside while-loop.

During this task i faced already infinity-loop problem, somehow manage to get pass it. Now my Euclid's algorithm is giving multiple answers (instead of one) and futhermore they are wrong...

I have searched a couple of topics over here, but none of answers shown in there gave me an answer. I tried to rewrite whole code, and also diffrent conditions for while-loop and if statement.

import java.lang.*;

class EuklidesAlgorithm {
    public static void main (String[] args) throws java.lang.Exception{
        int a = 25648;
        int b = 15468;

        while (a % b != 0 ){
            int modulo = a % b;
            if (modulo == 0){
                break;
            }else {
                modulo = a % b;
                System.out.println(" Checking for GCD");
                a = b;
                b = modulo;
            }
            System.out.println(" Number " + b + " is GDC of numbers(" + a + "," + b + ").");
        }
    }
}

I would like it to give a single answer what is GCD for a and b.

Aucun commentaire:

Enregistrer un commentaire