Dear All,
I have written a program to calculate the square root of an input number. It gives me an erroneous answer,for example, when I input the number 5, I get an answer 2.5 whereas I should get 2.24, same with other numbers. Below is the program, please point out bugs and fixes:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float error=.00001,g1=0.0,g2=0.0,m;
printf("Enter number whose square root has to be calculated");
scanf("%f",&m);
g2=m/2;
while (abs(g1-g2)<error)
{
g1=g2;
g2=(g1+(m/g1))/2;
}
printf("The square root of entered number is %f",g2);
system("pause");
return 0;
}