Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

greatest common divisor java

public class Main {
    public static void main(String[] args) {
        System.out.println(getGreatestCommonDivisor (25, 15)); 
        // output is 5
    }
    
    public static int getGreatestCommonDivisor (int first, int second) {
        if (first < 10 || second < 10) {
            return -1;
        }
        int GCD = 1;
        for (int i = 1; i <= first && i <= second; i++) {
            if ((first % i == 0) && (second % i == 0)) {
                GCD = i;
            }
        }
        return GCD;
    }
}
Source by www.baeldung.com #
 
PREVIOUS NEXT
Tagged: #greatest #common #divisor #java
ADD COMMENT
Topic
Name
7+7 =