quadratic equation

i dont know y my program is not working on linux system.
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;
}

i have type this code, there is no error but it says code is not working?
can anyoneo found the mistake?
What exactly says that the "code is not working"? What are the requirements that we should test against?

A quadratic equation can have two roots (-b +/- sqrt(b*b - 4ac)) / (2a), but you're only printing one. That's probably the first issue.
You also aren't dividing by 2a.

Perhaps give us some example input and output.
Last edited on
Write a program that calculates the real solution of the quadratic equation: a ∗ x2 + b ∗ x + c = 0
! First, read in the values for the parameters a, b, c (all of type float).
Then, the program should calculate and display the solution considering the following scenarios:
! a = 0 and b = 0 ⇒ display error message "invalid input"
! a = 0 and b ̸= 0 ⇒ x = −c
b
! (b2 − 4 ∗ a ∗ c) < 0 ⇒ display error message "no real solution"
! (b2 − 4 ∗ a ∗ c) ≥ 0 ⇒ x1 = −b+√b2−4∗a∗c
2∗a , x2 = −b−√b2−4∗a∗c
2∗a
Hint: The C++-function for y = √x is y = sqrt(x). Include math and use the function as in the example below:
#include <iostream>
#include <cmath>
using std::cout;
int main()
{
double a = 144.0; // define and initialize variable a
cout << "Result = " << sqrt(a) << endl; // display square root of a
return 0;
}


this is my problem
I edited my post at the same time you were writing that.

You aren't dividing by 2a for the case where the determinant is greater than 0.

Also, you should be using else if and not just if's.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (a == 0 && b == 0)
{
  // ...
}
else if (a == 0)
{
    // ...
}
else if ((b*b-4*a*c)>0)
{
    // ...
}
else if (/* ... */)
    // ... 
Last edited on
i have make the changes which u suggest but still it is not working
now i have make this change

x2=-b - sqrt(x1)/2*a;
x1=-b + sqrt(x2)/2*a;
C++ order of operations:
https://en.cppreference.com/w/cpp/language/operator_precedence

sqrt(x)/2*a is evaluated as (sqrt(x)/2)*a.

You need sqrt(x) / (2 * a).
Also, wait that still doesn't make sense. Why are you taking the output of the first call (x2) as the input of the second call?

It's
1
2
3
double det = (b*b)-(4*a*c);
x1 = (-b - sqrt(det)) / (2 * a);
x2 = (-b + sqrt(det)) / (2 * a);


Hope that clears it up... And again, when you say it "doesn't work", what do you actually mean?
Last edited on
why cant we upload any picture here?
so i can show u more accurate picture of my programe
Last edited on
Why don't you post a FULL and CODE-TAG-DELIMITED program?
im trying to cooy my coding from linux putty , but i failed. i dont know how to copy! :/
can you access it more normally using winscp?
pictures (esp when poorly cropped an scaled) often take up an insane amount of space for their contents. They also are difficult to police for ads or inappropriate content. An image of text is right on up there with XML for turning a couple of bytes into a couple of MILLION bytes.

and a lot of us can't (or will not) access third party sites for files.

get a local copy of your code, fix it in cygwin or something, and push it out to unix server once its working and done. Don't try to code on an unfriendly (not set up for it) server.
Last edited on
#include<iostream>
#include<cmath>
. .. . . .. .. .

cout "please enter value of a"
... by declaring all values. I started if else method
if
(a == 0 && b == 0) {
cout invalid equation
}
else if
(a == 0 && b != 0) {
x1 = -(c/b);
cout x1 << endl;
}
else if
(((b*b)-(4*a*c))< 0){
cout << no real solution < endl;
}
else if
((b*b)-(4*a*c) > 0) {
x1 = (-b - sqrt((b*b) - (4*a*c))) / (2 * a);
x2 = (-b + sqrt((b*b) - (4*a*c))) / (2 * a);
cout<< "x1 = "<< x1 << endl;
cout<< "x2 = "<< x2 << endl;
}
this is my code, it is compiling but not showing correct results, neither it is go inside last loop
can anyone tell me where is im making mistake?
That code as you have it does not compile. But ignoring that, one possible issue is when b*b - 4ac == 0 exactly.

Instead of
1
2
3
4
5
6
7
8
9
10
 
//...
else if ( b*b - 4*a*c < 0)
{
    // no real solution
}
else if (b *b - 4*a*c > 0)
{
   //
}


You should just have
1
2
3
4
5
6
7
8
9
10
 
// ...
else if ( b*b - 4*a*c < 0)
{
    // no real solution
}
else
{
   /// real solution(s)
}


Coding aside, I suggest setting up a compiler on your own computer so that you don't have to go through the pain of remoting in to test code. Compilers like GCC are supported on all major OSes.
Last edited on
thank u dear, i will follow ur suggestion :)
Topic archived. No new replies allowed.