Java program to find the greatest common divisor of given numbers

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

a little play
System.out.print("Enter first number: ");
int num1 = sc.nextInt();

System.out.print("Enter second number: ");
int num2 = sc.nextInt();

int max = (num1 > num2) ? num1:num2;

int maxDivisor = 0;

for(int i=1; i<=max/2; i++) {
if((num1 % i == 0) && (num2 % i == 0)) {
maxDivisor = i;
}
}

System.out.println("The greatest common divisor of the given numbers " + num1 + " and " + num2 + " = [ " + maxDivisor + " ]");

brankoblesicjr.
welcome to shbcf.ru