0% found this document useful (0 votes)
9 views13 pages

Ai1 10

The document contains a series of Prolog programs designed for various practical applications in Artificial Intelligence. These include demonstrating family relationships, user authentication with password attempts, disease diagnosis, calculating factorials and Fibonacci series, finding maximum values, list operations, solving the Water Jug problem, the Traveling Salesman problem, and implementing the Tower of Hanoi problem. Each section outlines the aim and provides the corresponding Prolog code along with expected outputs.

Uploaded by

210860116040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Ai1 10

The document contains a series of Prolog programs designed for various practical applications in Artificial Intelligence. These include demonstrating family relationships, user authentication with password attempts, disease diagnosis, calculating factorials and Fibonacci series, finding maximum values, list operations, solving the Water Jug problem, the Traveling Salesman problem, and implementing the Tower of Hanoi problem. Each section outlines the aim and provides the corresponding Prolog code along with expected outputs.

Uploaded by

210860116040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Enrollment no.

:230863116003 Artificial Intelligence(3161608)

Practical:-1
Aim:Write a Prolog program to demonstrate Family Relationship.
Prolog Program:
male(virat).
male(kameshwar).
male(yuvi).
female(sweety).
female(selvina).
female(kiran).
parent(kameshwar,virat).
parent(kameshwar,sweety).
parent(selvina,virat).
parent(selvina,sweety).
parent(yuvi,kameshwar).
parent(kiran,kameshwar).
sister(sweety,virat).
grandfather(yuvi,virat). grandfather(yuvi,sweety).
grandmother(kiran,virat). grandmother(kiran,sweety).
son(X,Y,Z):-male(X),parent(Y,X),parent(Z,X),female(Y),male(Z),!.
daughter(X,Y,Z):-female(X),parent(Y,X),parent(Z,X),female(Y),male(Z),!.
gfs(X,Y):-male(X),parent(X,Z),parent(Z,Y),male(Y),!.
gfd(X,Y):-male(X),parent(X,Z),parent(Z,Y),female(Y),!.
gms(X,Y):-female(X),parent(X,Z),parent(Z,Y),male(Y),!.
gmd(X,Y):-female(X),parent(X,Z),parent(Z,Y),female(Y),!.
gs(X,Y,Z):-male(X),male(Z),grandfather(Z,X),grandmother(Y,X),female(Y),!.
gd(X,Y,Z):-female(X),grandfather(Z,X),grandmother(Y,X),female(Y),!.

Information Technology 1
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Output:

Information Technology 2
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-2
Aim: (A) Give an opportunity to user to re-enter the password ‘n’ no. of
Times, on entering wrong password.
Prolog Program:
db(sweety,12345).
go(X,Y):-write('Enter username'),nl,read(X),write('Enter Password'),nl,read(Y).
login:-go(X,Y),db(X,Y),nl,write('your username and password
are'),nl,write(X),nl,write(Y).
login:-write('...Incorrect password, try again..'),login.
Output:

Information Technology 3
Enrollment no.:230863116003 Artificial Intelligence(3161608)

(B) Give an opportunity to user to re-enter the password three Times,


on entering wrong password.
Prolog Program:
db(sweety,2003).
db(virat,2005).
login:-login(0).
login(3):-write('maxx attempt cross'),!.
login(N):-goto(X,Y),db(X,Y),write('login successful');write('wrong uname and
pass'),nl,NN is N+1,login(NN).
goto(X,Y):-write('Enter username:'),read(X),write('Enter password:'),read(Y).
Output:

Information Technology 4
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-3
Aim: Write a Prolog program to implement an Expert System for
diagnosis the diseases.
Prolog Program:
symptom(john,fever).
symptom(john,rashes).
symptom(john,stomachache).
symptom(john,running_nose).
symptom(john,body_ache).
dis1(X,malaria):-symptom(X,fever),symptom(X,headache),symptom(X,running_nose).
dis2 (X,jaundice):-symptom(X,fever),symptom(X,stomachache),symptom(X,body_ache).
Output:

Information Technology 5
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-4
Aim: Write a Prolog program to find the factorial of a given number.
Prolog Program:
factorial(0,1).
factorial(N,F):-N>0,N1 is N-1,factorial(N1,F1),F is N*F1.
Output:

Information Technology 6
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-5
Aim: Write a Prolog program to find Fibonacci series.
Prolog Program:
go:-write('Enter term number : '),read(N),fibo(1,1,0,N).
fibo(_,_,_,0).
fibo(A,B,C,N):-AA is B,BB is C,CC is AA+BB,write(CC),NN is N-
1,fibo(AA,BB,CC,NN).
Output:

Information Technology 7
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-6
Aim:Write a Prolog program to find the maximum number from the
three numbers.
Prolog Program:
go:- write('Enter First no: '),read(A),write('Enter Second no: '),read(B),write('Enter Third
no: '),read(C),max(A,B,C).
max(A,B,C):-A>B,A>C,write('A is max').
max(A,B,C):-B>A,B>C,write('B is max').
max(A,B,C):-C>A,C>B,write('C is max').
Output:

Information Technology 8
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-7
Aim:Write a Prolog program based on list.
(A) To find the length of a list.
Prolog Program:
len_ls([],0).
len_ls([H|T],L):-len_ls(T,LL),L is LL+1.
Output:

(B) To find whether given element is a member of a list.


Prolog Program:
m1(E,[E|T]).
m1(E,[_|T]):-m1(E,T).
Output:

Information Technology 9
Enrollment no.:230863116003 Artificial Intelligence(3161608)

(C) To find reverse of a list.

Prolog Program:
reverse([],[]).
reverse([H|T],R):-reverse(T,RT),append(RT,[H],R).
Output:

(D) To add the member of a given list.

Prolog Program:
sum([],0).
sum([H|T],S):-sum(T,Temp),S is Temp+H.
Output:

Information Technology 10
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-8
Aim:Write a Prolog program to solve WATER-JUG problem.
Prolog Program:
go:-write('Enter capacity of X jug:'),read(X),write('Enter capacity of Y
jug:'),read(Y),jug(X,Y).
jug(X,Y):-X>4,write('Not Valid capacity'),!.
jug(X,Y):-Y>3,write('Not Valid capacity'),!.
jug(0,0):-write('(0,0)'),nl,jug(0,3).
jug(4,3):-write('(4,3)'),nl,jug(4,0).
jug(4,0):-write('(4,0)'),nl,jug(1,3).
jug(1,3):-write('(1,3)'),nl,jug(0,3).
jug(0,3):-write('(0,3)'),nl,jug(3,0).
jug(3,0):-write('(3,0)'),nl,jug(3,3).
jug(3,3):-write('(3,3)'),nl,jug(4,2).
jug(4,2):-write('(4,2)'),nl,jug(0,2).
jug(0,2):-write('(0,2)'),nl,jug(2,0).
jug(2,Z):-write('(2,'),write(Z),write(')').
Output:

Information Technology 11
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-9
Aim:Write a Prolog program for Travelling Salesman problem.
Prolog Program:
read('surat','valsad',100).
read('valsad','pune',150).
read('pune','mumbai',100).
read('chikhli','navsari',50).
find:-write('Source center:'),read(City1),write('Dest
center:'),read(City2),route(City1,City2,Dest).
route(City1,City2,Dist):-read(City1,City2,Dist),write(' Distance between
'),write(City1),write('and'),write(City2),write(' is '), write(Dist),nl,nl.
route(City1,City2,Dist):-read(City1,X,Dist1),route(X,City2,Dist2),Dist=Dist1+Dist2,
write(' Distance between '),write(City1),write(' and'),write(City2),write(' is
'),write(Dist),nl.
Output:

Information Technology 12
Enrollment no.:230863116003 Artificial Intelligence(3161608)

Practical:-10
Aim:Write a Prolog program to implement TOWER OF HANOI
problem.
Prolog Program:
move(1,X,Y,_):-write('Move disk from '),write(X),write(' to '),write(Y),nl.
move(N,X,Y,Z):-N>1,M is N-1, move(M,X,Z,Y), move(1,X,Y,_), move(M,Z,Y,X).
Output:

Information Technology 13

You might also like