Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: abc formule c++ code

1. ## abc formule c++ code

hi coders
well i just started coding in c/c++ and having some problems
i want to make a program which will solve the abc-formule
the normal abc formule is -b(-or+)√b^2-4*a*c/2a
but i cant get square root to work this is what i have till now
plz help me im kinda noob at coding :P
i changed the root square too V

# include <iostream.h>
# include <stdio.h>
# include <math.h>
main()
{
double a, b, c, d;
double ant1, ant2;
cout << "please enter the value of a here:9";
cin >> a;
cout << " please enter the value of b here:";
cin >> b;
cout << " please enter the value of c here:: ";
cin >> c;
d = ( b * b ) - ( 4 * ( a * c ) );
ant1= ( - b ) - ( Vd ) / ( 2 * a );
ant1= ( - b ) + ( Vd ) / ( 2 * a );
cout << ant1 ant2;

}
plz help me

• Have a look at the use of sqrt() function at http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

• ## Users who have thanked abduraooft for this post:

magreet3 (01-10-2012)

• Originally Posted by abduraooft
Have a look at the use of sqrt() function at http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
yeah i saw that page but i still dont know how to use it,
is it somthing like this:

# include <iostream.h>
# include <stdio.h>
# include <math.h>
main()
{
double a, b, c, d;
double ant1, ant2;
double Vd
cout << "please enter the value of a here:";
cin >> a;
cout << " please enter the value of b here:";
cin >> b;
cout << " please enter the value of c here:: ";
cin >> c;
d = ( b * b ) - ( 4 * ( a * c ) );
param = d;
result = sqrt (Vd);

ant1 = ( - b ) - ( Vd ) / ( 2 * a );
ant1 = ( - b ) + ( Vd ) / ( 2 * a );
cout << ant1, ant2;
}

• Close but you are performing the square root on the variable Vd but you never assign a value to the variable first. And you haven't declared the result variable.

Based on your other code you'd want to change it to:
Code:
`Vd = sqrt (d);`

• ## Users who have thanked oracleguy for this post:

magreet3 (01-10-2012)

• alright ty so now its all runing and stuff without errors but when i run it it shows the answers for like a milisecond and then leaves the program, if i use the code to wait till i hit a certain key, it do waits till i hit the key but isnt showing the result anymore
whats the good code to get the effect im looking for?
this is what i have now

# include <iostream.h>
# include <stdio.h>
# include <math.h>
main()
{
double a, b, c, d;
double ant1, ant2;
cout << "please enter the value of a here:";
cin >> a;
cout << " please enter the value of b here:";
cin >> b;
cout << " please enter the value of c here:";
cin >> c;
d = ( b * b ) - ( 4 * ( a * c ) );
ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
ant1 = ( - b ) + ( sqrt(d )) / ( 2 * a );
cout << ant1, ant2;
}

• okay now i have given it a delay at the end but its still not doing its job
it gives a hole different answer then it should be ):
can someone with some abc formule experience at the code for me

# include <iostream.h>
# include <stdio.h>
# include <math.h>
# include <windows.h>

main()
{
double a, b, c, d;
double ant1, ant2;
cout << "please enter the value of a here:";
cin >> a;
cout << " please enter the value of b here:";
cin >> b;
cout << " please enter the value of c here:";
cin >> c;
d = ( b * b ) - ( 4 * ( a * c ) );
ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
ant2 = ( - b ) + ( sqrt(d )) / ( 2 * a );
cout << ant1;
cout << ant2;
Sleep(10000);

}

• ## Square root issue.....

d=b*b-4ac;
double sqrtd; // this is variable which will save the calculation of d after taking Sqr Root
sqrtd=sqrt(d);

i think so enough....

• i dont think thats the problem cause its working now i saw the problem myself
when doing ant1 = ( - b ) - ( sqrt(d )) / ( 2 * a );
it first divided (2*a) with the sqrt and then removed it from -b well its should do it the other way, first -b- sqrt and then divided by (2 * a)
when i saw it i was rlly @facepalm, it was so simple
but heres my final code

# include <iostream.h>
# include <stdio.h>
# include <math.h>
# include <windows.h>

main()

{
double a, b, c, d, e;
double ant1, ant2, ant3, ant4;
cout << " please enter the value of A here:";
cin >> a;
cout << " please enter the value of B here:";
cin >> b;
cout << " please enter the value of C here:";
cin >> c;
d = ( b * b ) - ( 4 * ( a * c ) );
ant3 = ( - b ) - ( sqrt(d ));
ant4 = ( - b ) + ( sqrt(d ));
ant1 = ant3 / ( 2 * a );
ant2 = ant4 / ( 2 * a );
cout << ant1;
cout << " and ";
cout << ant2;
cout << " and now thanks brent :P";
cout << " D was:";
cout << d;
Sleep(20000);
}

thanks anybody who helped me (:

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•