Skip to content

Instantly share code, notes, and snippets.

@jukbot
Created November 28, 2015 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jukbot/32226dfb7a18c485dc36 to your computer and use it in GitHub Desktop.
Save jukbot/32226dfb7a18c485dc36 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class GCD {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Give me 2 numbers for GCD question: ");
int x = input.nextInt();
int y = input.nextInt();
int z = euclidgcd(x,y);
System.out.println("GCD of " + x + " and " + y + " are " + z);
}
public static int euclidgcd(int a, int b) {
int divisor = a < b?a : b; // if True it will return "a" if False will return "b"
int dividend = a < b?b:a;
int remainder = dividend % divisor;
while (remainder !=0) {
dividend = divisor;
divisor = remainder;
remainder = dividend%divisor;
}
return divisor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment