Given numbers a and b where 1<=a<=b, find the number of perfect squares between a and b(a and b inclusive)
#include <iostream>
using namespace std;
int main() {
   int a, b;
   int i=1, count=0;
   cout << "Enter two numbers a and b: ";
   cin >> a >> b;
   if ( a <= 0 ) {
       cout << "a must be greater than zero" << endl;
       return 0;
   }
   if (b < a) {
       cout << "b must be greater than a" << endl;
       return 0;
   }
   while (i*i < a) {
       i++;
   }
   while (i*i <= b) {
       count++;
       i++;
   }
   cout << "There ara " << count << " perfect squares between "
        << a << " and " << b << endl;
   return 0;
}
Comments
Leave a comment