Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The square root of a number N can be approximated by repeated calculation using

ID: 3539609 • Letter: T

Question

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 compute a value for NG using the formula given above. 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. Using an initial guess of 1.0;

Explanation / Answer


#include<stdio.h>

#include<stdlib.h>


int main()

{

int N;

float NG = 1;

float LG;


printf("Enter N : ");

scanf("%d",&N);


if(N <= 0)

{

printf("Entered value is <= 0 ");

exit(0);

}


do

{

LG = NG;

NG = 0.5*(LG + N/LG);

}while(NG - LG >= 0.05 || NG - LG <= -0.05);


printf("Sqrt(%d) = %f ", N, NG);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote