So I have coded this program on formula method .

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{


    int a , b , c ;



    cout << " Enter the values of a , b , c  " << endl;

    cin >> a ;
    cin >> b;
    cin >> c;

cout << "Therefore according to the formula : b*b-4ac =" << b*b-4*a*c << endl;
cout << "Therefore x = " << ((-b) + sqrt(b*b-4*a*c)) / (2*a) << " or " <<((-b) - sqrt(b*b-4*a*c)) / (2*a) << endl;
system("pause");
return 0;




}

It works perfectly except that it divides everything . I mean lets take an example of a = 3 , b = 7 , c = 4 .

So the ans which shows up are -1 or -1.33333 but I don't want the second ans in decimal . Rather I would like the second ans as -4/3 .

Any way of doing that ?

If you want to ouput the answer as a fraction you either need to code it that way or make some sort of rational number class

((-b) + sqrt(bb-4ac)) / (2a)

See that /? Don't do that. Assign numerator and denominator to separate variables and print them out.

num = ((-b) + sqrt(b*b-4*a*c))
den = (2*a)

Edited 4 Years Ago by WaltP

That worked uptill a certain extent . Can I make an if statement in which I can put something like num/den is not in decimals ?

If so how can I do that ?
Thank you :)

num = ((-b) + sqrt(b*b-4*a*c))
den = (2*a)

It's unlikely that sqrt(b*b-4*a*c) will be an integer (or even a rational number).

A better approach would be something like this:

...
p = -b
q = b*b-4*a*c
r = 2*a
...
print "x = ("
print p
print " +/- sqrt("
print q
print ")) / "
print r
...

Edited 4 Years Ago by m4ster_r0shi

It's unlikely that sqrt(bb-4a*c) will be an integer (or even a rational number).

OK, but some will be.

Edited 4 Years Ago by iamthwee

Ok, I don't disagree. I guess the OP could get the integer part of sqrt(b*b-4*a*c), square it, and see if it gives b*b-4*a*c. If yes, it's ok to use sqrt.

It's unlikely that sqrt(bb-4a*c) will be an integer (or even a rational number).

Oh yeah?

int sqt;
sqt = sqrt(b*b-4*a*c);
print sqt;

Looks integer to me...

I think "m4ster_r0shi" gave the best answer in his first post.

There is no guarantee that the results will be nice, round, numbers. Even if the square root function were to return a whole number, the next step is to subtract it from (-b) and then divide that result by (2a). Again, there is a good chance the result will be a decimal.

Other than compute the discriminant (b * b - 4 * a * c), this program does really little to forward you to the solutions of a quadratic equation.

Edited 4 Years Ago by DavidB

@waltp , how do I explain this .

Run my program , take a = 3 , b = 7 , c= 4 .
you will get x as -1 (which is correct) and 1.33333 (which is correct but in decimals)
If I do as you tell I get the ans as ,

x = -6/6 or x = 8/4

So can I make my program such that x = -1 and x = 8/4 .
?

@masterroshi - I don't really get what you are trying to imply . According to your code , how can I complete the answer . Wouldn't it just give me an incomplete ans ?

Sorry for my stupidness .

Edited 4 Years Ago by im abcd

At this point, I really don't know whether Walt is being serious or not. In any case, his suggestion is a good first step, but it's not enough. Let's see where it falls short. Run the following program and enter 1 3 -2 as input.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;
    int num, den;

    cin >> a >> b >> c;

    den = 2 * a;

    num = - b + sqrt(b * b - 4 * a * c);
    cout << "x1 = " << num << '/' << den << endl;

    num = - b - sqrt(b * b - 4 * a * c);
    cout << "x2 = " << num << '/' << den << endl;
}

You'll get 1/2 and -7/2, which, obviously, is wrong,
as ... (1/2)^2 + 3*( 1/2) - 2 == -1/4 != 0
and (-7/2)^2 + 3*(-7/2) - 2 == -1/4 != 0

The problem here is that b * b - 4 * a * c is 17. The square root of 17 is an irrational number (as is the square root of any prime number) and is approximately equal to 4.123. However, the decimal part is lost during the integer conversion that follows, and you get a wrong answer. The solution here is simple. Just don't use the square root in your output when the result is not an integer. How can you do that? Again, it's very simple:

int x = ...
int r = sqrt(x);
if (r * r == x) ok;
else problem;

About your other question, one way to turn 8/4 to 2 is to find the gcd of 8 and 4 and then divide each of them with it. After this, the denominator is 1, so, you can omit it. Same with turning -6/6 to -1, simplifying 6/4 to 3/2, etc...

Something else. You don't account for the case where b * b - 4 * a * c is negative. Make sure you fix this.

Edited 4 Years Ago by m4ster_r0shi

The only way to not get decimals is to use fractions. The only way to get only fractions is to not use sqrt() functions and no division.

The only way to get a square root without sqrt() AFAIK is to write a function to calculate the sqrt using an algorithm and stop as you get to the decimal portion. You shold not convert what's left into a fraction.

Sorry master roshi but I didn't quite understand what you said :( .
I coded this

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;


int main()

{
    int a , b , c;
    int num ;
    int den = 2*a;
    int x = b*b+4*a*c;
    int  r = sqrt(x);

    cout << " Enter the values for a , b , c  " << endl;
    cin >> a >> b >> c;

    cout << " According to the forumla b*b+4*a*c = " << x << endl;
    if(r*r == x)
    {
          num = -b+sqrt(x);
        cout << " x  = " << num <<  " / " << den << endl;

        cout << " Or " << endl;

        num = -b-sqrt(x);

        cout << " x = " << num <<  " / "  << den << endl;

    }
    else
    {
        cout << " error !" << endl;
    }

    system("pause");
return 0;


}

Which basically is messed up :P .

Lines 12-15 need to come after line 18. Otherwise you are doing calculations with variables that havent been set yet.

Yeap. Also, the correct formula is b*b - 4*a*c, not b*b + 4*a*c.

Now, instead of cout << " error !" << endl; you could put something like this:

cout << " x = [" << -b << " + sqrt(" << x << ")] / " << den << endl;
cout << " Or " << endl;
cout << " x = [" << -b << " - sqrt(" << x << ")] / " << den << endl;

I made these corrections and ran the program with 1 -5 6, 3 4 7 and 1 3 -2 as inputs. I got correct results in all cases.

But what you have now is only one of the three possible cases. There is also the case that b*b-4*a*c is zero and the case that b*b-4*a*c is less than zero. Do you know how to handle these?

Thank you master roshi , it works well :D and as you had said in one of your previous posts , to find the gcd ..How do I do that ?

From reading the wiki page for Greatest common divisor (GCF) it show to use Euclid's algorithm to find the GCF. The link that shows some pseudocode for using the algorithm is here.

And here is a recursive implementation.

int GCF(int a, int b)
{
    return ( (b == 0) ? a : GCF(b, a%b) );
}

it works well

It's not complete yet, though. An input of 1 0 1 could yield a much simpler output (i.e. x = i or x = -i). An input of 1 -4 4 should only give one solution. Also, an input of 0 for a should be rejected.

to find the gcd ..How do I do that ?

Here's some pseudocode. Make sure the arguments you pass in your gcd function are positive numbers. You could use abs for this.

EDIT: sfuo beat me to it...

Edited 4 Years Ago by m4ster_r0shi

This article has been dead for over six months. Start a new discussion instead.