Practical: 4 Write a program to find the roots of the quadratic equation.



domains
     A,B,C,Delta=integer
     Root1,Root2=real
predicates
     go
     root(Delta,A,B,C)
clauses
     go:-
                 write("Value of A."),nl,
                 readint(A),
                 write("Value of B."),nl,
                 readint(B),
                 write("Value of C."),nl,
                 readint(C),
                 Delta=B*B-4*A*C,
                 write("Delta = ",Delta),
                 root(Delta,A,B,C).
     root(Delta,A,B,_):-
                 Delta>0,
                 Root1=-B+sqrt(Delta)/2*A,
                 Root2=-B-sqrt(Delta)/2*A,
                 nl,
                 write("Root1 = ",Root1),nl,
                 write("Root2 = ",Root2),nl.
     root(Delta,_,_,_):-
                 Delta<0,
                 nl,
                 write("Roots are imaginary.").           
     root(Delta,A,B,_):-
                 Delta=0,
                 Root1=-B/2*A,
                 write("Root = ",Root1).

No comments:

Post a Comment