Find HCF or GCD Java

PROGRAM TO FIND THE HCF OR GCD OF TWO NUMBERS


public class GCD {

    public static void main(String[] args) {

        int n1 = 81, n2 = -153;

        // Always set to positive
        n1 = ( n1 > 0) ? n1 : -n1;
        n2 = ( n2 > 0) ? n2 : -n2;

        while(n1 != n2)
        {
            if(n1 > n2)
                n1 -= n2;
            else
                n2 -= n1;
        }

        System.out.println("G.C.D = " + n1);
    }
}

OUTPUT
G.C.D = 9

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java