#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.001
float f (float x)
{
return (x-exp(x*-1));
}
void main()
{
int ctr,temp,ctr1;
float res,res1;
int p,NO;
float X1=0.00,X2=0.00,prevX3;
float X3,FX1,FX2,FX3;
int co[10];
clrscr();
//For Scanning X1 and X2
printf("\nEnter the Roots:1::");
flushall();
scanf("%f",&X1);
printf("\nEnter the Roots:2::");
flushall();
scanf("%f",&X2);
//Greater than Zero roots
FX1=f(X1);
FX2=f(X2);
if((FX1 * FX2)>0)
{
printf("\nThis is not Proper Approximation");
}
ctr1=0;
clrscr();
printf("\n==========================================================");
printf("\nNO\tX1\tFX1\tX2\tFX2\tX3\tFX3");
printf("\n==========================================================");
do
{
prevX3=X3;
X3 = ((X1*FX2) - (X2*FX1)) / (FX2 - FX1);
FX1=FX2=FX3=0.0;
FX3 = f(X3);
FX1 = f(X1);
FX2 = f(X2);
printf("\n%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f",ctr1+1,X1,FX1,X2,FX2,X3,FX3);
if((FX1 * FX3)>0)
{
X1=X3;
}
elseif(FX3 == 0)
{
printf("%.4f",X3);
break;
}
else
{
X2=X3;
}
ctr1++;
}
while(fabs(prevX3-X3)>E);
printf("\n\n\tThe Root:: %.4f",X3);
getch();
}