Perfect square and perfect cube

前端 未结 8 2135
离开以前
离开以前 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: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.

提交回复
热议问题