C++ Program on square root of a number


Problem Description
Given a number A. Return square root of the number if it is perfect square otherwise return -1. Note: A number is a perfect square if its square root is an integer.

Problem Constraints
1 <= A <= 108

Input Format
First and the only argument is an integer A.

Output Format
Return an integer which is the square root of A if A is perfect square otherwise return -1.

/**
 * @input A : Integer
 * 
 * @Output Integer
 */
 
 #include<math.h>

int solve(int A) {
    int i;
    int sqrtNo = sqrt(A);

    if(sqrtNo*sqrtNo == A)
    {
            return sqrtNo;
    }
    else
    {
            return -1;
    }
}



MCQs

About Us | Contact Us | Privacy Policy | Career  | Online Training
Youtube   LinkedIn   Facebook   Twitter   Instagram  
Copyright©CppBuzz.com
Like many websites, we use cookies to ensure best browsing experience on our website. While using this website, you acknowledge to have read and accepted our cookie and privacy policy.