Advertisement
JosepRivaille

P88790: Màxim comú divisor iteratiu

Apr 4th, 2015
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int mcd(int a, int b) {
  6.     if (a < 0) a = -a;
  7.     if (b < 0) b = -b;
  8.     if (b > a) {
  9.         int aux = a;
  10.         a = b;
  11.         b = aux;
  12.     }
  13.     int r;
  14.     while (b != 0) {
  15.         r = a%b;
  16.         a = b;
  17.         b = r;
  18.     }
  19.     return a;
  20. }
  21.  
  22. //Pre: Llegeix 2 nombres
  23. //Post: N'escriu el mcd utilitzant un algorisme iteratiu
  24. int main() {
  25.     int a, b;
  26.     cin >> a >> b;
  27.     cout << mcd(a, b) << endl;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement