mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Greatest common divisor.
This commit is contained in:
parent
98005e3fde
commit
a82550d66f
20
misc/GCD.c
Normal file
20
misc/GCD.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Euclid's algorithm
|
||||
int GCD(int x, int y) {
|
||||
if (x == y || y == 0)
|
||||
return x;
|
||||
if (x == 0)
|
||||
return y;
|
||||
if (x > y)
|
||||
return GCD(x-y, y);
|
||||
else
|
||||
return GCD(x, y-x);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a, b;
|
||||
printf("Input two numbers:\n");
|
||||
scanf("%d %d", &a, &b);
|
||||
printf("Greatest common divisor: %d\n", GCD(a, b));
|
||||
}
|
Loading…
Reference in New Issue
Block a user