Write a C++ program to Solve Quadratic equation




cpp tutorials
cpp tutorials

Write a program that calculates the real solution of the quadratic equation ax²+bx+c=0
Read in the values for the parameters a,b,c(type float).
Then the program should calculate the solution considering the following circumstances:
a=0andb=0=>Not a valid equation
a=0 and b≠0 => x=-c/b
b² -4ac < 0 => Not a Real Solution
b² -4ac >0 => x1= (-b+√(b² -4ac))/2a , x1= (-b -√(b² -4ac))/2a
Hint:The c++ function for √x is sqrt(x).
To use this function you need to includemath.has a header file#include<math.h>

Solution-

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
    double a,b,c,x1,x2;
    char x;
    cout<<"enter the value of a=";
    cin>>a;
     cout<<"enter the value of b=";
    cin>>b;
     cout<<"enter the value of c=";
    cin>>c;
     cout<<"the quadratic equation is"<<a;
     cout<<"*x*x+"<<b;
     cout<<"*x+"<<c<<endl;
    if
        
                    (a==0 && b==0)
        cout<<"not a valid equation";
          if
        
                    (a==0 && b!=0)
                    {
                          x1=-(c/b);
                          
                 
    cout<<endl;
    cout<<"root="<<x1;
    cout<<endl; 
}
     if ((b*b-4*a*c)>0)
    {
        x2=(b*b)-(4*a*c);
         x1=-b+sqrt(x2);
         cout<<"root="<<x1<<endl;
         }
     if ((b*b-4*a*c)<0)
     {
    cout<<"not a real root"<<endl;
}
    system("pause");
    return 0;
}

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*