Advertisement
MeehoweCK

Untitled

Feb 5th, 2024
670
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.  
  3. using namespace std;
  4.  
  5. unsigned int nwd(unsigned int a, unsigned int b) {
  6.     if (a * b == 0) {
  7.         return 0;
  8.     }
  9.     while (a != b) {
  10.         if (a > b) {
  11.             a -= b;
  12.         }
  13.         if (b > a) {
  14.             b -= a;
  15.         }
  16.     }
  17.     return a;
  18. }
  19.  
  20. unsigned int nww(unsigned int a, unsigned int b) {
  21.     return a * b / nwd(a, b);
  22. }
  23.  
  24. int main() {
  25.     cout << "Podaj dwie liczby naturalne: ";
  26.     unsigned int a, b;
  27.     cin >> a >> b;
  28.     auto wynik{ nwd(a, b) };
  29.     if (wynik == 0) {
  30.         cout << a << " i " << b << " nie posiadaja wspolnego dzielnika\n";
  31.     }
  32.     else {
  33.         cout << "Najwiekszy wspolny dzielnik liczb " << a << " i " << b << " wynosi " << wynik << endl;
  34.         cout << "Najmniejsza wspolna wielokrotnosc tych liczb wynosi " << nww(a, b) << endl;
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement