Advertisement
BojidarDosev

zad2. Find the GCD of 3 numbers (recursion)!

Nov 23rd, 2023
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gcd1(int y, int z)
  5. {
  6.     if (z == 0)
  7.     {
  8.         return y;
  9.     }
  10.     else if (y == 0)
  11.     {
  12.         return z;
  13.     }
  14.     else
  15.     {
  16.         return gcd1(z, y % z);
  17.     }
  18. }
  19.  
  20.  
  21. int gcd(int x, int y, int z)
  22. {
  23.    
  24.     if (x == 0)
  25.     {
  26.         return x;
  27.     }
  28.     else if (z == 0)
  29.     {
  30.         return y;
  31.     }
  32.     else if (y == 0)
  33.     {
  34.         return z;
  35.     }
  36.     else
  37.     {
  38.         return gcd(y, x%y,gcd1(y,z));
  39.     }
  40. }
  41.  
  42. int main()
  43. {
  44.     //values for x,y,z
  45.     int x, y, z;
  46.     cin >> x; cin >> y; cin >> z;
  47.  
  48.    
  49.     if (x < 20 || x > 300 || y < 20 || y > 300 || z < 20 || z > 300)
  50.     {
  51.         cout << "Invalid input data!";
  52.         return 1;
  53.     }
  54.     cout << gcd(x, y, z);
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement