Advertisement
MeehoweCK

Untitled

May 6th, 2023
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int nwd(int a, int b)
  6. {
  7.     if (a < 0)
  8.         a = -a;
  9.     if (b < 0)
  10.         b = -b;
  11.     if (a * b == 0)
  12.         return 0;
  13.     while (a != b)
  14.     {
  15.         if (a > b)
  16.             a -= b;
  17.         if (b > a)
  18.             b -= a;
  19.     }
  20.     return a;
  21. }
  22.  
  23. int nww(int a, int b)
  24. {
  25.     return a * b / nwd(a, b);
  26. }
  27.  
  28. int main()
  29. {
  30.     cout << "Podaj dwie liczby calkowite: ";
  31.     int a, b;
  32.     cin >> a >> b;
  33.     int wynik = nwd(a, b);
  34.     if (wynik != 0)
  35.         cout << "Najwiekszy wspolny dzielnik " << a << " i " << b << " wynosi " << wynik << ", a najmniejsza wspolna wielokrotnosc " << nww(a, b) << ".\n" << endl;
  36.     else
  37.         cout << "Podane liczby nie maja wspolnych dzielnikow, a najmniejsza wspolna wielokrotnosc wynosi 0.\n";
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement