Question: #include / / Function to find square root of given number up to given precision float squareRoot ( int number, int precision ) { int

#include
// Function to find square root of given number up to given precision
float squareRoot(int number, int precision){
int start =0, end = number;
int mid;
// variable to store the answer
float ans;
// for computing integral part of square root of number
while (start <= end){
mid =(start + end)/2;
if (mid * mid == number){
ans = mid;
break;
}
// incrementing start if integral part lies on right side of the mid
if (mid * mid < number){
start = mid +1;
ans = mid;
}
// decrementing end if integral part lies on the left side of the mid
else {
end = mid -1;
}
}
// For computing the fractional part of square root up to given precision
float increment =0.1;
for (int i =0; i < precision; i++){
while (ans * ans <= number){
ans += increment;
}
// loop terminates when ans * ans > number
ans = ans - increment;
increment = increment /10;
}
return ans;
}
// Driver code
int main(){
// Function calling
printf("%.*f
",3, squareRoot(50,3));
// Function calling
printf("%.*f
",4, squareRoot(10,4));
return 0;
}
please explain this C code and also manual trace it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!