Quadratic equation in Prolog
Example for versions
B-Prolog 7.4,
gprolog 1.3.0,
swipl 5.6.x
This is an ISO Prolog example, using standard read/1 predicate for reading input. Note that when using read/1, you have to put full stop . after each value you input.
q :- write('A = '),
read(A),
( A = 0, write('Not a quadratic equation');
write('B = '),
read(B),
write('C = '),
read(C),
D is B*B-4*A*C,
( D = 0, write('x = '), X is -B/2/A, write(X);
D > 0, write('x1 = '), X1 is (-B+sqrt(D))/2/A, write(X1), nl, write('x2 = '), X2 is (-B-sqrt(D))/2/A, write(X2);
R is -B/2/A, I is abs(sqrt(-D)/2/A),
write('x1 = ('), write(R), write(', '), write(I), write(')'), nl,
write('x1 = ('), write(R), write(', -'), write(I), write(')')
)
).
Comments
]]>blog comments powered by Disqus
]]>