Sunteți pe pagina 1din 2

Programe C++

CMMDC CU ALGORITMUL LUI EUCLID (cu do while)


#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "CMMDC CU ALGORITMUL LUI EUCLID (cu do while) \n ";
cout << "\nIntroduceti primul numar: "; cin >> a;
cout << "Introduceti al doilea numar: "; cin >> b;
do
{
int r=a%b;
a=b;
b=r;
}
while (a%b!=0);
cout << "\ncmmdc este: " << b << endl << endl;
return 0;
}

CMMDC CU ALGORITMUL LUI NICHOMEDE (cu while)


#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "CMMDC CU ALGORITMUL LUI NICHOMEDE (cu while) \n ";
cout << "\nIntroduceti primul numar: "; cin >> a;
cout << "Introduceti al doilea numar: "; cin >> b;
while (a!=b)
if (a>b)
a=a-b;
else
b=b-a;
cout << "\ncmmdc este: " << a << endl << endl;
return 0;
}
CMMMC (cu for - 2)
#include <iostream>
using namespace std;
int main()
{
int a, b, r, m;
cout << "CMMMC (cu for - 2) \n ";
cout << "\nIntroduceti primul numar: "; cin >> a;
cout << "Introduceti al doilea numar: "; cin >> b;
for ( ; a%b!=0; r=a%b,a=b,b=r);
m=(a*b)/b;
cout << "\ncmmmc este: " << m << endl << endl;
return 0;
}