Answer to Question #288512 in C++ for Prajwal

Question #288512

Given numbers a and b where 1<=a<=b, find the number of perfect squares between a and b(a and b inclusive)


1
Expert's answer
2022-01-19T16:29:48-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment