Advertisement
lewapkon

NWW_NWD

Nov 18th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main (){
  8.     long a,b;
  9.  
  10.     cout << "Podaj pierwsza liczbe: " << endl;
  11.     cin >> a;
  12.    
  13.     cout << "Podaj druga liczbe: " << endl;
  14.     cin >> b;
  15.  
  16.     vector<int> reszty;
  17.     reszty.push_back(a);reszty.push_back(b);
  18.  
  19.     //Obliczanie NWD
  20.  
  21.     for( ; reszty[reszty.size() - 1] != 0 ; ){
  22.         reszty.push_back(reszty[reszty.size() - 2] % reszty[reszty.size() - 1]);
  23.     }
  24.    
  25.     long nwd = reszty[reszty.size() - 2];
  26.  
  27.     //Obliczanie NWW
  28.  
  29.     long nww;
  30.     nww = a*b / nwd;
  31.  
  32.     //Wyświetlenie wyników
  33.  
  34.     cout << "\nNajwiekszy wspolny dzielnik dla tych liczb wynosi: " << nwd << endl;
  35.     cout << "Najmniejsza wspolna wielokrotnosc dla tych liczb wynosi: " << nww << endl << endl;
  36.    
  37.     system("pause");
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement