GCD Euclids way





GCD represents the greatest common divisor. The GCD is defined as the two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. GCD also known as the greatest common factor (GCF), or highest common factor (HCF).

GCD (Greatest Common Divisor ) Euclids way
public class GreatestCommonDivisor {

//using euclid’s way of calculating GCD
static int greatestCommonDivisor(int a, int b) {
int gdivisor = 1, divider, dividend;
if (a < b) { // the one less is divisor the one greater is dividend
divider = a;
dividend = b;
}
else {
divider = b;
dividend = a;
}

if (dividend % divider == 0) { //its the GCD
return divider;
}
else { //proceed further
greatestCommonDivisor(divider, dividend % divider);
}
return gdivisor; //the GCD of the 2 numbers
}

public static void main(String args[]) {
System.out.println(greatestCommonDivisor(5, 144));
}
}
GCD (Greatest Common Divisor ) Euclids way

if you find any bug in the above program. let us know about it. Please post your complaint at pctech@gadgetcage.com.


We'll send more interesting posts like GCD Euclids way to you!
Enter your Email Address:
Join us on Facebook.

Speak Your Mind

*