Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 19, 2019 04:48
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 parzibyte/639e49692bcb9f57b38743f74d14d090 to your computer and use it in GitHub Desktop.
Save parzibyte/639e49692bcb9f57b38743f74d14d090 to your computer and use it in GitHub Desktop.
/**
* Máximo común divisor en C con el algoritmo de Euclides
* implementado con ciclo while y con recursión / recursividad
* @date 2019-12-18
* @author parzibyte
* @see https://parzibyte.me/blog
* */
#include <stdio.h>
int maximo_comun_divisor(int a, int b) {
int temporal;//Para no perder b
while (b != 0) {
temporal = b;
b = a % b;
a = temporal;
}
return a;
}
int maximo_comun_divisor_recursivo(int a, int b) {
if (b == 0) return a;
return maximo_comun_divisor_recursivo(b, a % b);
}
int main(void) {
printf("MCD de 50 y 120: %d\n", maximo_comun_divisor(50, 120));
printf("MCD de 50 y 120 (recursivo): %d\n", maximo_comun_divisor_recursivo(50, 120));
printf("MCD de 7 y 5: %d\n", maximo_comun_divisor(7, 5));
printf("MCD de 7 y 5 (recursivo): %d\n", maximo_comun_divisor_recursivo(7, 5));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment