Es un metodo que sirve para hallar el mcd de dos numeros enteros
positivos. El metodo fue creado por el matematico de origen griego llamado
Euclides.
Codigo en C++
int alg_euc(int a,int b)
{
int max,min,r;
// identificamos el mayor y menor de los numeros
if(a>=b)
{max=a;min=b;}
else
{max=b;min=a;}
while(min!=0)
{
r=max%min;
max=min;
min=r;
}
return max;
}
Llamamos a nuestra funcion desde nuestro metodo main
int main(int argc, char *argv[])
{
int a,b,mcd;
cout<<" Algoritmo de Euclides\n\n";
cout<<" ingrese a : ";cin>>a;
cout<<"\n ingrese b : ";cin>>b;
mcd=alg_euc(a,b);
cout<<"\n el mcd de "<<a<<" y "<<b<<" es "<<mcd<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
No hay comentarios:
Publicar un comentario