Hi i am just starting out with c++ and i have a problem, i tried to make a program that solves second degree equtations of this form: ax^2+bx+c=0.
and here is the code:
#include <iostream>
#include <cmath>
usingnamespace std;
int main() {
double a,b,c,delta,x1,x2,x; //declaring variables
cout << "This program solve the equation: ax^2 + bx + c=0"<<endl<<"Enter coefficient a:";
cin >>a;
cout << endl<<"Enter coefficient b:";
cin >> b;
cout << endl<<"Enter contstant c:";
cin >> c;
delta = b*b-4*a*c;
cout << "delta = "<<delta<<endl;
if (delta==0)
x = (-b)/(2*a);
cout<<"There is one solution: x = "<<x<<endl;
if (delta > 0)
x1 = (-b - sqrt (delta) )/(2*a);
cout<<"First solution: x1 = "<<x1<<endl;
x2 = (-b + sqrt (delta) )/(2*a);
cout <<"Second solution: x2 = "<<x2<<endl;
if (delta < 0)
cout<<"There is no real solution, there is two complex solutions.";
return 0;
}
when deta=0 it gives this output:
This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:1
Enter coefficient b:2
Enter contstant c:1
delta = 0
There is one solution: x = -1
First solution: x1 = 1.91474e-307
Second solution: x2 = -1
i don't want the last 2 lines to appear.
and when delta > 0 the program shows this:
This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:2
Enter coefficient b:5
Enter contstant c:2
delta = 9
There is one solution: x = nan
First solution: x1 = -2
Second solution: x2 = -0.5
i dont want "There is one solution: x = nan" to be shown.
while if delta<0 i have:
This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:6
Enter coefficient b:2
Enter contstant c:5
delta = -116
There is one solution: x = nan
First solution: x1 = 1.91474e-307
Second solution: x2 = nan
There is no real solution, there is two complex solutions.