Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- float f(float x)
- {
- return (pow(x,3)-4*x-9);
- }
- void main( )
- {
- float a=-1.0, b=0.0, x=0.0, y=0.0, err;
- while(f(a)*f(b)>0)
- {
- a=b;
- b=b+1.0;
- }
- printf(βEnter the error: β);
- scanf(β%fβ,&err);
- printf("\n\troot lies between: %f and %f",a,b);
- printf("\n\n\t a(-ve)\tb(+ve)\t\t x\t\t f(x)");
- printf("\n\t-------------------------------------------------------------\n");
- //*swapping the values*//
- if(f(a)>0)
- {
- a=a+b;
- b=a-b;
- a=a-b;
- }
- //* Finding the approximate value of the root*//
- do
- {
- y=x;
- x=(a+b)/2;
- if(f(x)<0)
- a=x;
- else
- b=x;
- printf("\n\t%f\t%f\t%f\t %f",a,b,x,f(x));
- }while(fabs(x-y) > err);
- printf("\n\t-------------------------------------------------------------\n");
- printf("\n\tAnswer: %.4f (correct upto 4 decimal places)",x);
- getch( );
- }
Add Comment
Please, Sign In to add comment