Write a C++ prgm to find roots by using quadratic formula?
@ajprincess
sorry i'm not very good at c :(
i think u should try yourself first and then if you face any problem,i will help you. otherwise i can give you source code but that would not be good for you
JAVA JAVA :P
I will help with C, it works on C++, but if you want in C++, just make some changes (little). Look, you said quadratic, with Math it's like : \[Ax^{2} + Bx + C\] Where A,B and C are constants. So we need the following variables : A, B, C to represent the constant, and another variable for the DELTA, we called it D, and another two variables for the solutions, X1,X2. Lets start : #include<stdio.h> #include<stdlib.h> #include<math.h> // so we can use the function sqrt(). // We declare our variables here; int A = 0,B = 0,C = 0, D; float X1 = 0,X2 = 0; // We will make a function to calculate Delta. int Delta (int a, int b, int c){ D = B*B - (4 * A * C); return D; } // Now our main program int main(){ printf("Enter the value of A : "); //The user will enter the value of A,B and C. scanf("%d", &A); printf("Enter the value of B : "); scanf("%d", &B); printf("Enter the value of C : "); scanf("%d", &C); D = Delta(A,B,C); if (D<0) { printf("There is no solution\n"); //In case Delta is inferior than 0. }else if (D == 0) { X1 = -B/(2*A); printf("There is one solution : X = %f\n", X1); }else { // Delta > 0 X1 = (-B + Sqrt(D))/2*A; //Calculate X1, and X2 X2 = (-B - Sqrt(D))/2*A; printf("There are two solutions : X1 = %f and X2 = %f\n", X1,X2); } system("pause"); } Hope that's help you. Good luck.
@ktobah may be you missed brackets around 2*A in the case with Delta >0 : (2*A)
No @nick67 , it's ok there because I already putted (-B + Sqrt(D)) between brackets.
@ktobah sorry ktobah, but the way you wrote the expression works this way: \[\left( -B+\sqrt{D} \right)/2*A\]that is, the content of brackets is first divided by 2, then the partial result is multiplied by A; we instead would like that the content in brackets should divided by the product 2*A \[(-B+\sqrt{D})/(2*A)\]:-)
Ah yeah true, good remark, thanks. May the asked change this in his program.
you're welcome
All i can say is try to enumerate how would someone will solve it like a trial and error first. This trial and error is equivalent to if else statements. Under it should be the process of each way to solve it. Hope that helped someone. xD
Join our real-time social learning platform and learn together with your friends!