Algorithm:
Program:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float n;
float f(float x)
{ return(x * x - n); }
main()
{
float a, b, epsilon, xmid;
clrscr();
printf("\nEnter the no. of which square root has to be found :\n");
scanf("%f",&n);
printf("\nEnter the initial guess values and accuracy: \n");
scanf("%f,%f,%f",&a, &b, &epsilon);
if(f(a) * f(b) > 0)
{
printf("\n Unsuitable initial values \n"); exit(0);
}
xmid = (a + b) / 2;
while(fabs(xmid - b) / xmid >= epsilon)
{
if(f(xmid) == 0)
{ break; }
if(f(a) * f(xmid) < 0)
{b = xmid;}
else
{a = xmid;
xmid = (a + b) / 2;}
}
printf("\nThe Square root = %2f \n",xmid);
getch(); return 0;
}
Run 1:
Enter the no. of which square root has to be
found :
25
Enter the initial guess values and accuracy:
4,6,0.0001
The Square root = 5.000000
Run 2:
Enter the no. of which square root has to be
found :
49
Enter the initial guess values and accuracy:
4,6,0.0001
Unsuitable initial values
0 Comments