Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- /* Prints roots of the quadratic equation a * x*x + b * x + c = 0 */
- void solveQuadraticEquation(double a, double b, double c){
- double root1, root2;
- double discriminant = findDiscr(a,b,c);
- if (discriminant < 0)
- {
- printf("No roots\n");
- }
- else
- {
- root1 = solvRootOne(a,b,discriminant);
- root2 = solvRootTwo(a,b,discriminant);
- }
- }
- int main(void) {
- double a,b,c;
- do
- {
- printf("Enter coeficients a, b, and c: ");
- scanf("%lf %lf %lf", &a, &b, &c);
- solveQuadraticEquation(a, b, c);
- }
- while (!(a==0 && b == 0 && c == 0));
- return 0;
- }
- double findDiscr(double a, double b, double c)
- {
- double discriminant = b * b - 4 * a * c;
- return discriminant;
- }
- double solvRootOne(double a,double b,double discriminant)
- {
- double root1;
- if (discriminant == 0){
- root1 = -b/(2*a);
- }
- else
- {
- root 1 = (-b + sqrt(discriminant))/(2*a);
- }
- return root1;
- }
- double solveRootTwo(double a,double b,double discriminant)
- {
- double root2;
- if discriminant > 0
- {
- root2 = (-b + sqrt(discriminant))/(2*a);
- }
- else
- {
- root2 = (-b - sqrt(discriminant))/(2*a);
- }
- return root2;
- }
Add Comment
Please, Sign In to add comment