Untitled

 avatar
unknown
plain_text
3 years ago
722 B
2
Indexable
#include <iostream>
#include<math.h>
using namespace std;

float EqnSatisfy(float c)
{
    float ans;
    ans=pow(c,3)-2*(c)-5;
    if(ans==0)
        return 0;
    return ans;
}
int main()
{
    float a,b;
cout<<"Equation is x^3-2x-5 \n";
cout<<"Enter the starting interval of equation ";
cin>>a;
cout<<"\nEnter the end interval of equation ";
cin>>b;
int count=1;
float c=-1;
while(count<=9)
{
    cout<<count<<" Iteration\n";

    c=(a+b)/2;
    cout<<"Value of x"<<count<<"is "<<c<<"\n";
    if(EqnSatisfy(c)==0){
        cout<<"Exact Root is "<<c;
        return 0;
    }else
    {
     if(EqnSatisfy(a)*EqnSatisfy(c)<0)
            b=c;
     else
        a=c;
    }
    count++;
}
cout<<"Root is "<<c;
   return 0;
}
Editor is loading...