How to calculate the HCF (Highest Common Factor) of two integers in Dart

1 Answer

0 votes
// Highest common factor (HCF) = the largest integer that two or more numbers can be divided by
 
class MyClass
{
    static int HFC(int divided, int divisor) {
        int? remainder;
        var hcf = 0;
        do {
            remainder = divided % divisor;
            if (remainder == 0) {
                hcf = divisor;
            }
            else {
                divided = divisor;
                divisor = remainder;
            }
        } while (remainder != 0);
         
        return hcf;
    }
     
    static void main()
    {
        var divided = 18;
        var divisor = 27;
         
        print("HCF = " + (MyClass.HFC(divided, divisor)).toString());
    }
}
 
void main() {
    MyClass.main();
}
 
 
 
 
/*
run:
 
HCF = 9
 
*/

 



answered Apr 27, 2023 by avibootz
edited Jan 9, 2024 by avibootz
...