逻辑概念:
The format of a quadratic equation is:ax²+bx+c=0(a≠0)
The formula for finding the root is Δ = b²-4×a×c
This program needs to use the header file #include <> among the sqrt()function (math.)。
The sqrt() function is used to compute a non-negative real numberThe square root of (i.e., the open root sign).
Code Example:
-
#include <>
-
#include <>
-
int main()
-
{
-
float a,b,c,q;
-
float x1,x2;
-
printf("Please enter the a:");
-
scanf("%f",&a);
-
printf("Please enter the b:");
-
scanf("%f",&b);
-
printf("Please enter the c:");
-
scanf("%f",&c);
-
q = b*b - 4*a*c;
-
x1 = (-b+sqrt(q)) / (2*a);
-
x2 = (-b-sqrt(q)) / (2*a);
-
printf("\n equation is %.1fx² + %.1fx + %.1f = 0\n",a,b,c);
-
if(q < 0)
-
printf("∵ Δ < 0\n∴ The equation has no solution\n");
-
else if(q == 0)
-
printf("∵ Δ = 0\n∴ x1=x2=%.2f",x1);
-
else
-
printf("∵ Δ = %.2f\nOpening the root sign gives %.2f\n∴ x1=%.2f\nx2=%.2f\n",q,sqrt(q),x1,x2);
-
return 0;
-
}
Input Example:
-
Please enter the a:5
-
Please enter the b:7
-
Please enter the c:2
Example output:
-
Please enter the a:5
-
Please enter the b:7
-
Please enter the c:2
-
-
The equation is5.0x²+7.0x+2.0=0
-
∵ Δ = 9.00
-
lit. opening the root sign gets3.00
-
∴ x1=-0.40
-
x2=-1.00