Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- float calc(float x)
- {
- return x*x*x-4*x-9;
- }
- void func(float a, float b)
- {
- float fa=calc(a),fb=calc(b),fc;
- float c,pc=-1;
- int p,itr;
- float tol;
- if(fa*fb>0)
- {
- int a1,b1;
- printf("Try Again ! ! !\n");
- printf("Enter 2 values");
- scanf("%f%f",&a1,&b1);
- func(a1,b1);
- }
- else if(fa*fb==0)
- {
- if(fa==0)
- printf("root=%f\n",a);
- if(fb==0)
- {
- printf("root=%f\n",b);
- }
- }
- else
- {
- printf("Enter precision and iteration");
- scanf("%f%d",&tol,&itr);
- for(int i=0;i<itr;i++)
- {
- c=(a+b)/2;
- fc=calc(c);
- if(fc==0)
- printf("root=%d\n",c);
- else if(fa*fc<0)
- {
- b=c;
- }
- else
- {
- a=c;
- }
- if(fabs(c-pc)<tol)
- {
- printf("solution %f \n",c);
- break;
- }
- if(i==itr-1)
- printf("Need more iteration");
- pc=c;
- }
- }
- }
- int main()
- {
- printf("Enter 2 val");
- float a,b;
- scanf("%f%f",&a,&b);
- func(a,b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement