Advertisement
makispaiktis

Sherlock and Watson problem

Apr 6th, 2019 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int maxNumber(int n1, int n2){
  9.     if(n1 >= n2)
  10.         return n1;
  11.     else
  12.         return n2;
  13. }
  14.  
  15. int minNumber(int n1, int n2){
  16.     if(n1 >= n2)
  17.         return n2;
  18.     else
  19.         return n1;
  20. }
  21.  
  22. int main()
  23. {
  24.     cout << "Give me 2 numbers and between them I will find all the perfect squares between them.\n";
  25.     int n1;
  26.     int n2;
  27.     cin >> n1;
  28.     cin >> n2;
  29.     cout << endl;
  30.     int maximum = maxNumber(fabs(n1), fabs(n2));
  31.     int minimum = minNumber(fabs(n1), fabs(n2));
  32.     //cout << maximum << "  " << minimum << endl;
  33.     vector <int> squares = vector <int>();
  34.     for(int i=minimum; i<maximum+1; i++){
  35.         if(sqrt(i) == (int)(sqrt(i))){
  36.             squares.push_back(i);
  37.         }
  38.     }
  39.  
  40.     // Display all elements
  41.     cout << squares.size() << " perfect squares found:\n";
  42.     for(unsigned int i=0; i<squares.size(); i++){
  43.         cout << squares[i] << endl;
  44.     }
  45.  
  46.     cout << endl;
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement