Prolog program to demonstrate the fail predicates and cut predicates. - Artificial intelligence
- Program to demonstrate the fail predicates.
Program:
predicates
start
info(string,string)
clauses
start:-
info(Name,City),
writef("%-10%2\n",Name,City),
fail.
start.
info("Dharmik","Ahmedabadbad").
info("Krutik","gandhinagar").
Output:
Goal: start
Dharmik Ahmedabad
Krutik Gandhinagar
- Program to demonstrate the cut predicates.
Program:
predicates
start
info(string,string)
cut(string)
clauses
start:-
info(Name,City),
cut(Name),
writef("%-10%2\n",Name,City),
fail.
start.
cut("Rutu"):-
!,fail.
cut(_).
info("Dharmik","Ahmedabadbad").
info("Rutu","himmatnagar").
info("Krutik","gandhinagar").
Output:
Goal: start
Dharmik Ahmedabad
Krutik Gandhinagar
Prolog program to find the square root of given number. - Artificial intelligence
- Program to find the square root of given number.
Program:
domains
N=integer
I=integer
predicates
start
sq1(N,I)
sq2(N,I)
clauses
start:-
write("Enter
number="),
readint(N),
sq1(N,1).
sq1(N,I):-
(I*I)>=N,
sq2(N,I).
sq1(N,I):-
I1=I+1,
sq1(N,I1).
sq2(N,I):-
(I*I)>N,
write("Enter square
number!\n"),
start.
sq2(_,I):-
write("square root
is ",I),
write("\n").
Output:
Goal:
Start
Enter
number=8
Enter
square number!
Enter
number=9
Square
root is 3
Yes
Goal:
Prolog program to check whether a given number is Armstrong or not. - Artificial intelligence
- program to check whether a given number is Armstrong or not.
Program:
armst(X) :-
N is X,
R is X mod 10,
S is 0,
arm(N,R,S,X).
arm(N,R,S,X):-
N > 0,
S1 is S+R*R*R,
N1 is N div 10,
R1 is N mod 10,
arm(N1,R1,S1,X).
arm(N,_,S,X):-
N = 0,
S\=X,
write("Armstrong").
arm(N,_,S,X):-
N = 0,
S\=X,
write("Not Armstrong").
Output:
?- armst(1634).
Armstrong
true
prolog programs for finding factorial of numbers and generating a Fibonacci series. - - Artificial intelligence
- program for finding a factorial of given number (N!)
Program
fact(X,Y):-
X is
0,
Y is 1;
X>0,
N is X-1,
fact(N,G),
Y is X*G.
Output
?-
fact(6,X).
X
= 720 .
?-
fact(8,X).
X
= 40320
- program for generating a Fibonacci series.
Program
fib(0,0).
fib(1,1).
fib(N,F):-
N>1,
N1 is N-1,
N2 is N-1,
fib(N1,F1),
fib(N2,F2),
F is F1+F2.
Output
%?- fib(5,F).
%F = 16 .
Prolog program to find the greatest variable among the three variable. - Artificial intelligence
- Write a program to find the greatest variable among the three variable
Program
max(P,Q,R):-P>Q,P>R,write('Larger
number is '),write(P).
max(P,Q,R):-P<Q,Q>R,write('Larger
number is '),write(Q).
max(P,Q,R):-R>Q,P<R,write('Larger
number is '),write(R).
max(P,Q,R):-P=Q,P<R,write('Larger
number is '),write(R).
max(P,Q,R):-P<Q,P=R,write('Larger
number is '),write(Q).
max(P,Q,R):-Q=R,P>Q,write('Larger
number is '),write(P).
max(P,Q,R):-P=Q,P=R,write('All
numbers are equal ').
max(P,Q,R):-P=Q,P>R,write('Larger
numbers are '),write(P),write(' and
'),write(Q).
max(P,Q,R):-P=R,Q<R,write('Larger
numbers are '),write(P),write(' and
'),write(R).
max(P,Q,R):-Q=R,P<R,write('Larger
numbers are '),write(R),write(' and
'),write(Q).
Output
?-
max(1,2,3).
Larger
number is 3
true
.
?-
max(2,2,2).
All
numbers are equal
true.
Prolog program to find the roots of the quadratic equation. - Artificial intelligence
- program to find the roots of the quadratic equation.
Program
delta(A,
B, C, D):- D is B*B - 4*A*C.
equation(A,B,C,X)
:-
delta(A,B,C,D1),
( D1
< 0
-> X is 0
; D1
=:= 0
-> X is -B/2*A
; X is
-B-sqrt(D1)/2*A
).
Output
?-
equation(1,2,3,X).
X
= 0.
?-
equation(1,4,3,X).
X
= -5.0.
?-
equation(1,4,4,X).
X
= -2.
Prolog program for login without and with recursion. - Artificial intelligence
- Logon example without recursion.
Program
go:-
write("Enter user name and password\n"),
readln(User),
readln(Pass),
login(User,Pass);
write("Login sucessful\n").
go:-
write("Login sucessful\n").
login(dharmik,123).
login(nihar,456).
Output
?- go().
Enter user name and password
|: dharmik
|: 123
Login sucessful
true.
- Logon example with recursion.
Program
go:-
write("Enter user name and
password\n"),
readln(User),
readln(Pass),
login(User,Pass),
write("Login sucessful\n").
go:-
write("Login unsucessful\n"),
go.
login(dharmik,123).
login(nihar,456).
Output
?-
go().
Enter
user name and password
|:
dharmik
|:
456
Login
unsucessful
Enter
user name and password
|:
dharmik
|:
123
Login
sucessful
true
Subscribe to:
Posts
(
Atom
)
No comments :
Post a Comment