Saturday, 25 February 2017

GATE EXAM PREPARATION IN "C" - function

Consider the following function
double f(double x)
{
   if (abs(x*x - 3) < 0.01) return x;
   else return f(x/2 + 1.5/x);
}
Give a value q (to 2 decimals) such that f(q) will return q:_____.
Answer: 1.732
Explanation:
The main thing to note is the expression “abs(x*x – 3) < 0.01" inside the if condition. The function would return x when x2 is close to 0 (smaller than 0.01) which means when x is close to square root of 3. Square root of 3 is 1.732.

No comments:

Post a Comment