Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 12:39
Show Gist options
  • Save jimmy087/5517378 to your computer and use it in GitHub Desktop.
Save jimmy087/5517378 to your computer and use it in GitHub Desktop.
Greatest Common Divisor in java
import java.util.Scanner;
// Calculating Greatest Common Divisor.
public class GreatestCommomDivisor {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the first integer: ");
int number1 = input.nextInt();
System.out.print("Please enter the second integer: ");
int number2 = input.nextInt();
int gcd = 1;
for (int k = 2; k <= number1 && k <= number2; k++) {
if(number1 % k == 0 && number2 % k == 0) gcd = k;
}
System.out.print("The greatest common divisor for " + number1 + " and " + number2 + " is " + gcd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment