Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

common greatest divisor java

int gcdByBruteForce(int n1, int n2) {
    int gcd = 1;
    for (int i = 1; i <= n1 && i <= n2; i++) {
        if (n1 % i == 0 && n2 % i == 0) {
            gcd = i;
        }
    }
    return gcd;
}
Comment

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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java word count 
Java :: java days between two dates 
Java :: Java forName() method 
Java :: selet all elemnts 
Java :: java doreturn void 
Java :: android studio close app 
Java :: java yes or no loop 
Java :: how to print two variables in same line in java 
Java :: sort array java 
Java :: java date time 
Java :: java biginteger multiply 
Java :: how-to-check-internet-connection-in-android 
Java :: create arraylist from array java 
Java :: java thread lambda 
Java :: java how to print 
Java :: how to loop through a 2d array java 
Java :: java checking for null 
Java :: for each java 
Java :: spring boot sql logging 
Java :: kill all java processes linux 
Java :: capitalize each word in string java 
Java :: caesar cipher java 
Java :: java decimalformat 
Java :: sort array based on string length java 
Java :: java uuid 
Java :: groovy ternary operator short form 
Java :: gradle reflections 
Java :: toast no android 
Java :: can we create a class inside a class in java 
Java :: get text from a jtextfield 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =