|
3. Find the real root of the equation ax ^ 2 + bx + c = 0, a, b, c are entered by the keyboard, a is not equal to 0 and b ^ 2-4ac> 0 (formula method)
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define Pi 3.14
void main ()
{
int a, b, c;
double x1, x2, s;
printf ("Input a, b, c:\n");
scanf ("% d% d% d",&a,&b,&c);
while (a == 0 || (b * b-4 * a * c) <= 0) / * If the input is illegal, return and enter again
{
printf ("Input a, b, c error!\nInput a, b, c again:\n");
scanf ("% d% d% d",&a,&b,&c);
}
s = b * b-4 * a * c;
x1 = ((-b) + sqrt (s)) / (2.0 * a);
x2 = ((-b) -sqrt (s)) / (2.0 * a);
printf ("% dx ^ 2 +% dx +% d = 0\n", a, b, c); / * show equation * /
printf ("x1 =% f x2 =% f\n", x1, x2);
getch ();
} |
|