Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Quadratic Equations

C Language Examples - Quadratic Equations

C Language Examples C Language Examples

Solve the quadratic equation:ax2+bx+c=0 roots.

Enter the values of three real numbers a, b, c, with a not equal to 0.

Example

#include

<

stdio.h

#include

<

math.h

int

main

(

)

{

float

a

,

b

,

c

,

x1

,

x2

,

d

;

printf

(

"

Enter the three coefficients of the equation:

"

)

;

scanf

(

"

%f %f %f

"

,&

a

,&

b

,&

c

)

;

if

(

a

!=

0

)

{

d

=

sqrt

(

b

b

4

a

c

)

;

x1

=

(

b

d

)

/

(

2

a

)

;

x2

=

(

b

d

)

/

(

2

a

)

;

if

(

x1

<

x2

)

printf

(

"

%0.2f %0.2f

\

n

"

,

x2

,

x1

)

;

else

printf

(

"

%0.2f %0.2f

\

n

"

,

x1

,

x2

)

;

}

return

0

;

}

Running result:



输入方程的三个系数:1 2 1

-1.00 -1.00

C Language Examples C Language Examples