W
W
whatislov2020-12-21 22:53:50
C++ / C#
whatislov, 2020-12-21 22:53:50

How to find the GCD of two numbers?

How to find the greatest common divisor of two given numbers? I wrote the code for finding a common divisor, but I can’t select the common divisor.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int x1,x2,k,i=1,c=1;
    setlocale(LC_ALL,"Russian");
   cout<<"Введите первое число\n";
   cin>>x1;
    cout<<"Введите второе число\n";
   cin>>x2;
   cout<<"Введите максимальное число\n";
   cin>>k;
   while(i<=k){
   if((x1%i==0 && x2%i==0) && ((x1%i==0)==(x2%i==0))){
       c=i;
        cout<<"Общий делитель -> "<<c<<endl;
      }
      i++;
   }
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2020-12-21
@whatislov

Read about Euclid's algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question