Solution to Quadratic Equations Write a program in C languag
Solution to Quadratic Equations
Write a program in C language with a loop that solves quadratic equations with coefficients read from the terminal.
Need Part 1 and Part 2 to be done.
Solution
Solution of Problem 1 : part 1,2,3
#include<stdio.h>
#include<math.h>
int computeSolutions(double a, double b, double c, double *px1, double *px2)
{
double d;
d = pow(b,2)-4*a*c;
if(d < 0)
{
*px1 = 0;
*px2 = 0;
return 0;
}
else if(d == 0)
{
*px1 = (-b)/(2*a);
*px2 = *px1;
return 1;
}
else
{
*px1 = (-b-sqrt(d))/(2*a);
*px2 = (-b+sqrt(d))/(2*a);
return 2;
}
}
int readCoefficients(double *pa, double *pb, double *pc)
{
if(scanf(\"%lf\",pa)==1)
{
if(scanf(\"%lf\",pb)==1)
{
if(scanf(\"%lf\",pc)==1)
return 3;
else
return 2;
}
else
return 1;
}
else
return 0;
}
int main()
{
double a,b,c,px1,px2;
int d,x;
while(1)
{
x = readCoefficients(&a,&b,&c);
if(x == 3)
{
d = computeSolutions(a,b,c,&px1,&px2);
if(d == 0)
printf(\"-- no real solutions\ \");
else if(d == 1)
printf(\"-- %d real solution: x1=x2=%.5lf\ \",d,px1);
else
printf(\"-- %d real solutions: x1=%.5lf x2=%.5lf\ \",d,px1,px2);
}
}
return 0;
}

