Advertisement
Rakibul_Ahasan

Scant_Method

Mar 9th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. float func(float x){
  5.     return (x*x)-(4*x)-10;
  6. }
  7.  
  8. int main()
  9. {
  10.     float a,b;
  11.     //cin>>a>>b;
  12.     scanf("%f%f",&a,&b);
  13.  
  14.     Step:
  15.     float f1=func(a);
  16.     float f2=func(b);
  17.  
  18.     float r=((f2*a)-(f1*b))/(f2-f1);
  19.  
  20.     //cout<<a<<"   "<<b<<"   "<<"   "<<f1<<"   "<<f2<<"   ";
  21.     printf("a: %.4f\tb: %.4f\tf1(a): %.4f\t\tf2(b):%.4f\t",a,b,f1,f2);
  22.  
  23.     if(abs((r-b)/r)>0.03){
  24.         printf("Iterative is%.4f\n",r);
  25.         a=b;
  26.         f1=f2;
  27.         b=r;
  28.         f2=func(r);
  29.         goto Step;
  30.     }
  31.     else  printf("Root is: %.4f\n",r);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement