The square root of a number N can be approximated by repeated calculation using
ID: 3856161 • 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 C language 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.Write a complete C language program to test your square root function for the following numbers: 4. 120.5.36.01. and 0.25 (Use an initial guess of 1.0).Explanation / Answer
//this a c program to find the square root of the number uisng described method
#include<stdio.h>
main(){
float N;
//taking input from the user
printf("Enter any number:");
scanf("%f",&N);
float LG,NG,temp;
LG=1.0;//assigning initial guess as 1.0
//the method starts from here
while(1){
NG=(0.5)*(LG+(N/LG));
printf("%f ",NG);
if(LG>NG){
temp=LG-NG;
}
else{
temp=NG-LG;
}
if(temp<=0.005f){
break;
}
else{
LG=NG;
}
}
//Displaying the output
printf(" The Root of your number is:%f",NG);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.