nvnnaidenov

Zad53

Oct 15th, 2021
732
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int gdc(int x, int y)
  6. {
  7.     while(x != y)
  8.     {
  9.         if(x > y)
  10.         {
  11.             x = x - y;
  12.         }
  13.         else
  14.         {
  15.             y = y - x;
  16.         }
  17.         return x;
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     int a;
  24.     int b;
  25.     int c;
  26.     int d;
  27.     int r;
  28.     int s;
  29.  
  30.     cout << "a = ";
  31.     cin >> a;
  32.     cout << "b = ";
  33.     cin >> b;
  34.     cout << "c = ";
  35.     cin >> c;
  36.     cout << "d = ";
  37.     cin >> d;
  38.     if(a < 1 || b < 1 || c < 1 || d < 1)
  39.     {
  40.         cout << "Error, bad input!" << endl;
  41.         return 1;
  42.     }
  43.  
  44.     r = gdc(a, b);
  45.     cout << "gdc(" << a << ", " << b << ") " << r << endl;
  46.  
  47.     s = gdc(c, d);
  48.     cout << "gdc(" << c << ", " << d << ") " << s << endl;
  49.  
  50.     cout << "gdc(" << a << ", " << b << ", " << c << ", " << d << ") " << gdc(r, s) << endl;
  51.  
  52.     return 0;
  53.  
  54. }
RAW Paste Data