Advertisement
MeehoweCK

Untitled

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