#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
float f(float x)
{
return pow(x,4)+2pow(x,3)+pow(x,2)-5;
}
int main(int argc, char *argv[])
{
double a,b;
double m,n,i,sum,root;
double esp=1e-8;
printf("Please enter the root interval[a,b]:");
scanf("%lf %lf",&a,&b);
i=0;
if(f(a)*f(b)<0)
{
while(b-a>esp)
{
m=(a+b)/2;
sum=f(m);
if (sum==0)
{
i++;
break;
}
else if (f(a)*f(m)<0)
{
m=b;
i++;
}
else
{
m=a;
i++;
}
}
}
else
{
printf("There are no roots in this interval.\n");
}
root=(a+b)/2;
printf("Number of iterations is %d\n",i);
printf("The approximate root of the equation is %lf\n",root);
system("PAUSE");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float f(float x)
{
return pow(x,4)+2*pow(x,3)+pow(x,2)-5;
}
int main(int argc, char *argv[])
{
double a,b;
double m,n,i,sum,root;
double esp=1e-8;
printf("Please enter the root interval[a,b]:");
scanf("%lf %lf",&a,&b);
i=0;
if(f(a)*f(b)<0)
{
while(b-a>esp)
{
m=(a+b)/2;
sum=f(m);
if (sum==0)
{
i++;
break;
}
else if (f(a)*f(m)<0)
{
m=b;
i++;
}
else
{
m=a;
i++;
}
}
}
else
{
printf("There are no roots in this interval.\n");
}
root=(a+b)/2;
printf("Number of iterations is %d\n",i);
printf("The approximate root of the equation is %lf\n",root);
system("PAUSE");
return 0;
}
float f(float x)
{
return pow(x,4)+2*pow(x,3)+pow(x,2)-5;
}