67.9k views
2 votes
In c the square root of a number N can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for next guess and LG stands for last guess. Write a function that calculates the square root of a number using this method. The initial guess will be the starting value of LG. The program will com- pute a value for NG using the formula given. The difference between NG and LG is checked to see whether these two guesses are almost identical. If they are, NG is accepted as the square root; otherwise, the next guess (NG) becomes the last guess (LG) and the process is repeated (another value is computed for NG, the difference is checked, and so on). The loop should be repeated until the difference is less than 0. 005. Use an initial guess of 1. 0. Write a driver function and test your square root function for the numbers 4, 120. 5, 88, 36.01, 10,000, and 0. 25

PLEASE İN C PROGRAMMİNG

1 Answer

4 votes

Answer:

Following are the program to the given question:

#include <stdio.h>//header file

double square_root(double N, double initialGuess)//defining a method square_root that takes two variable in parameters

{

double NG, LG = initialGuess,diff;//defining double variable

while(1)//use loop to calculate square root value

{

NG = 0.5 * (LG + N / LG);//using given formula

diff = NG - LG;//calculating difference

if(diff < 0)//use if to check difference is less than 0

diff = -diff;//decreaing difference

if(diff < 0.005)//use if that check difference is less than 0.005

break;//using break keyword

else//defining else block

{

LG = NG;//holding value

}

}

return NG;//return value

}

int main()//defining main method

{

double ans, n,initialguess = 1.0;//defining double variable

n = 4;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \\", n, ans);//print calculated value with number

n = 120.5;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \\", n, ans);//print calculated value with number

n = 36.01;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \\", n, ans);//print calculated value with number

n = 0.25;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \\", n, ans);//print calculated value with number

printf("\\Enter a number: ");//print message

scanf("%lf", &n);//input value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \\", n, ans);//print calculated value with number

}

Output:

Please find the attachment file.

Step-by-step explanation:

  • In this code, a method "square_root" is declared that takes two variable "N, initialGuess" in its parameters, inside the method a three double variable is declared.
  • It uses the given formula and uses the diff variable to hold its value and uses two if to check its value is less than 0 and 0.005 and return its calculated value.
  • In the main method, three double variables are declared that use the "n" to hold value and "ans" to call the method that holds its value and print its value.
In c the square root of a number N can be approximated by repeated calculation using-example-1
User Chris Berlin
by
3.3k points