Created
December 19, 2019 04:48
-
-
Save parzibyte/639e49692bcb9f57b38743f74d14d090 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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