← Каталог
Java — консольные задачи — 5.1. НОД — алгоритм Евклида
Фрагмент из «Java — консольные задачи»: 5.1. НОД — алгоритм Евклида.
import java.util.Scanner;
public class Gcd {
static int gcd(int a, int b) {
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return Math.abs(a);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(gcd(a, b));
sc.close();
}
} import java.util.Scanner;
public class Gcd {
static int gcd(int a, int b) {
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return Math.abs(a);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(gcd(a, b));
sc.close();
}
}