Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void display_quadratic (float a, float b, float c);
float root1(float a, float b, float c);
float root2(float a, float b, float c);
float discriminant (float a, float b, float c);
float r1,r2;
int
main (void)
{
float a,b,c;
printf("This program solves the quadratic equation ax^2 + bx + c == 0.\n");
printf("Enter a, b, and c:");
scanf("%g%g%g", &a, &b, &c);
printf("OK, the equation you're specifying is:");
display_quadratic(a,b,c);
printf(" == 0\n");
r1 = root1(a,b,c);
r2 = root2(a,b,c);
printf("The roots of your equation are %g and %g, and the factorization is:\n ", r1,r2);
display_quadratic(a,b,c);
printf(" == ");
if (r1 > 0)
{
printf("(x - %g)", r1);
}
else
printf("(x + %g)", (r1 * -1));
if (r2 > 0)
{
printf("(x - %g)", r2);
}
else
printf("(x + %g)", (r2 * -1));
}
void display_quadratic (float a, float b, float c)
{
if (a == 1){
printf("x^2 ");}
else printf("%gx^2 ", a);
if (b == 1){
printf("x ");}
else printf("%gx ", b);
printf("%g ", c);
}
float discriminant (float a, float b, float c)
{
return (pow(b,2) - 4 * a * c);
}
float root1 (float a, float b, float c)
{
float root1_calculator;
root1_calculator = (-b + sqrt(discriminant(a,b,c)))/2 * a;
return (root1_calculator);
}
float root2 (float a, float b, float c)
{
float root2_calculator;
root2_calculator =(-b - sqrt(discriminant(a,b,c)))/2 * a;
return (root2_calculator);
}