Search⌘ K
AI Features

Forget the sqrt() Function

Test your understanding of C programming by predicting the output of a code puzzle that avoids the sqrt function. Learn to analyze C code closely and improve your problem-solving abilities through practical exercise and discussion.

We'll cover the following...

Puzzle code

Read carefully the code given below:

C
#include <stdio.h>
double babylonian(double r)
{
double low, high;
int x;
const int precision = 7;
low = 1.0;
high = r;
for( x=0; x<precision; x++ ) {
high = (high+low)/2.0;
low = r/high;
}
return(low);
}
int main()
{
double pv, sr;
printf("Enter a positive value: ");
scanf("%lf", &pv);
if( pv <= 0 )
return(1);
sr = babylonian(pv);
printf("The result is %f\n", sr);
return(0);
}

Your task: Guess the output

Attempt the following test to assess your understanding.

Technical Quiz
1.

What will be the output of the above code if the input is 7?

A.
The result is 2.645751
B.
The result is 2.645714
C.
The result is 2.236068
D.
The result is 2.228427

1 / 1

Let's discuss the code and output together in the next lesson.