Computer Engineering: languag: \"C Programming\" please make sure the code does
ID: 3586010 • Letter: C
Question
Computer Engineering:
languag: "C Programming"
please make sure the code does not have errors and is running before sending it.
thank you ^_^
2. Heron's Method (heron.c) Heron's Method provides an iterative technique to compute the square root of a number that converges to a very precise answer in very few iterations To demonstrate just how fast this formula converges to the same answer provided by the math library's built-in square root function, write a program that will use the function specified below (squareRootByHeron) to calculate the square root of a number double squareRootByHeron(int noOfIterations, double n); The squareRootByHeron function takes a number for setting the iteration count and a number n for calculating its square root. Output should be the Heron's approximation using the given number of iteration The driver program displays the output obtained from the Heron's method and the value obtained from the math library's square root function Following is the formula for Heron's method. Here n is the number for which we want to calculate the square root value. Initially xo is set to 2, and x1 is updated in each iteration. Final value of x1 provides the square root value of n. This formula should beExplanation / Answer
#include <stdio.h>
#include <math.h>
double squareRootByHeron(int numberOfIterations, double n) {
int i;
double x = n/2;
for(i=0;i<numberOfIterations;i++) {
x = (x + n/x)*0.5;
}
return x;
}
int main()
{
int count;
double n;
printf("Enter a number for computing its square root value: ");
scanf("%lf", &n);
printf("Enter iteration count: ");
scanf("%d", &count);
printf("Square root of %lf by Heron's Method: %lf ", n, squareRootByHeron(count, n));
printf("Square root of %lf by Math Library sunction: %lf ", n,sqrt(n));
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.