d3vdpro d3vdpro - 1 year ago 229
C++ Question

Perfect square and perfect cube

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..

Answer Source

No, but it's easy to write one:

bool is_perfect_square(int n) {
    if (n < 0)
        return false;
    int root(round(sqrt(n)));
    return n == root * root;

bool is_perfect_cube(int n) {
    int root(round(cbrt(n)));
    return n == root * root * root;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download