Created
January 6, 2014 15:45
-
-
Save pandey-adarsh147/8284695 to your computer and use it in GitHub Desktop.
Euclid's Algorithm for GCD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EuclidAlgo { | |
public static void main(String ...arg) { | |
Scanner scanner = new Scanner(System.in); | |
int m = scanner.nextInt(); | |
int n = scanner.nextInt(); | |
System.out.println("GCD: "+ gcd(m, n)); | |
} | |
private static int gcd(int m, int n) { | |
int r = m % n; | |
int q = m / n; | |
if(r == 0) return n; | |
return gcd(n, r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment