Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

Make efficient use of your practice time?

Answered: 1 week ago