Fri. Feb 23rd, 2024

Euclid algorithm

In mathematics, the Euclidean algorithm, or Euclid’s algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in Euclid’s Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a calculation according to well-defined rules, and is one of the oldest algorithms in common use. It can be used to reduce fractions to their simplest form, and is a part of many other number-theoretic and cryptographic calculations.

// filename: algoritmulEuclidIntregi.cpp
// borland c++ 5.0
// algoritmul lui Euclid pentru numere intregi
// marius ionescu, november 12, 2005


int cmmdc(long int,long int);
void ecran(void);

void ecran()
{ clrscr();
  cout<<"(1) Algoritmul lui Euclid";
  cout<<"(2) Iesire din program";
  cout<<"Optiunea= ";

int cmmdc(long int a,long int b)
{ int temp;
  if(a==0&&b!=0) return a;
  if(a!=0&&b==0) return b;
  if(a==0&&b==0) return 0;
  { temp=a;
  { temp=a%b;
  return b;

void main()
{ int option;
  float number1,number2; // protectie la introducere de reali
  { ecran();cin>>option;clrscr();

    if(option==2) exit(1);
    cout<<"Dai primul numar: "; cin>>number1;
    if(number1!=floor(number1)) {cout<<"acesta nu este un numar intreg!!!!"; getch(); exit(1); } // pt reali
    cout<<"Dai al doilea numar: "; cin>>number2;
    if(number1!=floor(number1)) {cout<<"acesta nu este un numar intreg!!!!"; getch(); exit(1); } // pt reali
    cout<<"\nCMMDC este: "<<cmmdc(number1,number2)<<endl;  // se permite doar transmiterea de valori
                                                           // intregi prin cele doua conditii
    cout<<"\napasa o tasta!";

411,722 total views, 1 views today