Advertisement
paulogp

Algoritmo de Euclides (MDC)

Jul 13th, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* algoritmo de euclides (mdc) */
  5. #include <stdio.h>
  6.  
  7. // metodo recursivo
  8. int func_mdc_r (int the_n1, int the_n2)
  9. {
  10.     return(the_n2 > 0 ? func_mdc_r(the_n2, the_n1 % the_n2): the_n1);
  11. }
  12.  
  13. // metodo iteractivo
  14. int func_mdc_i (int the_n1, int the_n2)
  15. {
  16.     int the_temp;
  17.  
  18.     while (the_n2 > 0)
  19.     {
  20.         the_temp = the_n2;
  21.         the_n2 = the_n1 % the_n2;
  22.         the_n1 = the_temp;
  23.     }
  24.  
  25.     return the_n1;
  26. }
  27.  
  28. int main (int argc, const char * argv[])
  29. {
  30.     /* mdc: maximo divisor comum algoritmo de Euclides */
  31.     int the_n1, the_n2, the_result;
  32.  
  33.     printf("Introduza N1: ");
  34.     scanf("%i", &the_n1);
  35.  
  36.     printf("Introduza N2: ");
  37.     scanf("%i", &the_n2);
  38.  
  39.     the_result = func_mdc_i(the_n1, the_n2);
  40.     printf("result i: %i\n", the_result);
  41.  
  42.     the_result = func_mdc_r(the_n1, the_n2);
  43.     printf("result r: %i\n", the_result);
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement