Numerical Methods for Engineers

Published by McGraw-Hill Science/Engineering/Math
ISBN 10: 0073401064
ISBN 13: 978-0-07340-106-5

Chapter 2 - Programming and Software - Problems - Page 48: 2.7b

Answer

#include # include int main(void) { $\ \ \ \ $ double y,a,e,sqroot; $\ \ \ \ $ scanf("%lf",&a); $\ \ \ \ $ if(a>0) $\ \ \ \ $ { $\ \ \ \ $$\ \ \ \ $ double tol=0.000001; $\ \ \ \ $$\ \ \ \ $ double x=a/2; $\ \ \ \ $$\ \ \ \ $ do $\ \ \ \ $$\ \ \ \ $ { $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $y=(x+a/x)/2; $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $e=abs((y-x)/y); $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $x=y; $\ \ \ \ $$\ \ \ \ $ } while (e>=tol); $\ \ \ \ $$\ \ \ \ $ sqroot=x; $\ \ \ \ $ } $\ \ \ \ $ else sqroot=0; $\ \ \ \ $ printf("%g",sqroot); $\ \ \ \ $ return 0; }

Work Step by Step

#include # include int main(void) { $\ \ \ \ $ double y,a,e,sqroot; $\ \ \ \ $ scanf("%lf",&a); $\ \ \ \ $ if(a>0) $\ \ \ \ $ { $\ \ \ \ $$\ \ \ \ $ double tol=0.000001; $\ \ \ \ $$\ \ \ \ $ double x=a/2; $\ \ \ \ $$\ \ \ \ $ do $\ \ \ \ $$\ \ \ \ $ { $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $y=(x+a/x)/2; $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $e=abs((y-x)/y); $\ \ \ \ $$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $x=y; $\ \ \ \ $$\ \ \ \ $ } while (e>=tol); $\ \ \ \ $$\ \ \ \ $ sqroot=x; $\ \ \ \ $ } $\ \ \ \ $ else sqroot=0; $\ \ \ \ $ printf("%g",sqroot); $\ \ \ \ $ return 0; }
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.