HomoCivicus

TUSUR 27.05.2019

May 27th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. // Наибольший общий делитель 2 чисел
  4. int gcd(int x, int y);
  5.  
  6. int main() {
  7.     int numbers[4];
  8.    
  9.     // Ввод 4 натуральных чисел
  10.     for (int i = 0; i < 4; i++) {
  11.         do {
  12.             cout << "Число №" << i + 1 << ": ";
  13.             cin >> numbers[i];
  14.         } while (numbers[i] <= 0);
  15.     }
  16.  
  17.     // Вывод
  18.     cout << "\nНОД чисел " << numbers[0] << ", " << numbers[1] << ", " <<
  19.         numbers[2] << ", " << numbers[3] << ": " <<
  20.         gcd(gcd(numbers[0], numbers[1]), gcd(numbers[2], numbers[3]));
  21.  
  22.     return 0;
  23. }
  24.  
  25. int gcd(int x, int y) {
  26.     // Алгоритм Евклида
  27.     while (x != y) {
  28.         if (x > y) {
  29.             x -= y;
  30.         }
  31.         else {
  32.             y -= x;
  33.         }
  34.     }
  35.  
  36.     return x;
  37. }
Add Comment
Please, Sign In to add comment