Advertisement
shivansh_joshi

Untitled

Mar 1st, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4. float calc(float x)
  5. {
  6.     return x*x*x-4*x-9;
  7. }
  8.  
  9. void func(float a, float b)
  10. {
  11.     float fa=calc(a),fb=calc(b),fc;
  12.    float c,pc=-1;
  13.     int p,itr;
  14.     float tol;
  15.     if(fa*fb>0)
  16.     {
  17.         int a1,b1;
  18.         printf("Try Again ! ! !\n");
  19.         printf("Enter 2 values");
  20.         scanf("%f%f",&a1,&b1);
  21.         func(a1,b1);
  22.     }
  23.     else if(fa*fb==0)
  24.     {
  25.         if(fa==0)
  26.             printf("root=%f\n",a);
  27.         if(fb==0)
  28.         {
  29.             printf("root=%f\n",b);
  30.         }
  31.            
  32.     }
  33.     else
  34.     {
  35.         printf("Enter precision and iteration");
  36.         scanf("%f%d",&tol,&itr);
  37.        
  38.         for(int i=0;i<itr;i++)
  39.         {
  40.             c=(a+b)/2;
  41.            
  42.             fc=calc(c);
  43.            
  44.             if(fc==0)
  45.                 printf("root=%d\n",c);
  46.             else if(fa*fc<0)  
  47.             {
  48.                 b=c;
  49.             }
  50.             else
  51.             {
  52.                 a=c;
  53.             }
  54.             if(fabs(c-pc)<tol)
  55.             {
  56.                 printf("solution  %f \n",c);
  57.                 break;
  58.             }
  59.  
  60.             if(i==itr-1)
  61.             printf("Need more iteration");
  62.            
  63.             pc=c;
  64.            
  65.         }
  66.  
  67.     }
  68.    
  69. }
  70.  
  71. int main()
  72. {
  73.     printf("Enter 2 val");
  74.     float a,b;
  75.     scanf("%f%f",&a,&b);
  76.     func(a,b);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement