Please correct the program: #include <iostream> #include <cmath> using namespace
ID: 3872512 • Letter: P
Question
Please correct the program:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
bool complex;
complex = false;
float x = 0; // x = -b/(2a)
float y = 0; // y = sqrt(radicand)/(2a)
float radicand;
if (a = !0) {
radicand = b * b - 4 * a * c;
}
if (radicand < 0) {
complex = true;
radicand = -radicand;
}
else if (b = !0) {
x = -c / b;
}
else if (a = 0 && b = !0) {
b * z + c = 0;
}
cout << "z = " << x << " + i " << fabs(y) << endl;
cout << "z = " << x << " - i " << fabs(y) << endl;
cout << "z = " << x + y << endl;
cout << "z = " << x - y << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include<cmath>
using namespace std;
int main() {
float a,b,c,d,root1,root2; // Declaring coefficients and roots
cin>>a>>b>>c; // Taking coefficients as input
if(a==0)
{
if(b==0)
{
if(c==0)
{
cout<<"All numbers are root";
}
}
else
{
cout<<"This is a linear equation"<<"root="<<-c/b;
}
}
else
{
d=b*b-4.0*a*c;
if(d>0.0)
{
d=sqrt(d);
root1=(-b+d)/(2.0*a);
root2=(-b-d)/(2.0*a);
cout<<"Roots are"<<root1<<"and"<<root2;
}
else if(d==0.0)
{
cout<<"Repeated root is",-b/2.0*a;
}
else
{
cout<<"There is no real root";
cout<<"Discriminant is"<<d;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.