Perfect square and perfect cube

前端 未结 8 2125
离开以前
离开以前 2020-12-09 12:27

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

相关标签:
8条回答
  • 2020-12-09 13:00

    Try this:

    #include<math.h>
    int isperfect(long n)
    {
        double xp=sqrt((double)n);
        if(n==(xp*xp))
            return 1;
        else
            return 0;
    }
    
    0 讨论(0)
  • 2020-12-09 13:10

    The most efficient answer could be this

        int x=sqrt(num)
        if(sqrt(num)>x){
        Then its not a square root}
        else{it is a perfect square}
    

    This method works because of the fact that x is an int and it will drop down the decimal part to store only the integer part. If a number is perfect square of an integer, its square root will be an integer and hence x and sqrt(x) will be equal.

    0 讨论(0)
提交回复
热议问题