0% found this document useful (0 votes)
27 views16 pages

Ai Lab Manual

The document outlines various experiments in Prolog programming, covering topics such as family relations, address listing, quadratic equations, finding minimum and maximum values, solving the water jug problem, calculating factorials, and more. Each experiment includes code snippets and example outputs demonstrating the functionality of the programs. The final experiments showcase advanced concepts like the Tower of Hanoi problem and calculating net salary for employees.

Uploaded by

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

Ai Lab Manual

The document outlines various experiments in Prolog programming, covering topics such as family relations, address listing, quadratic equations, finding minimum and maximum values, solving the water jug problem, calculating factorials, and more. Each experiment includes code snippets and example outputs demonstrating the functionality of the programs. The final experiments showcase advanced concepts like the Tower of Hanoi problem and calculating net salary for employees.

Uploaded by

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

EXPERIMENT NO.

Write a program which contains three predicates: male, female, parent.


Make rules for following family relations: father, mother, grandfather,
grandmother, brother, sister, uncle, aunt, nephew and niece.

female(niyati).
female(leena).
female(stuti).
female(dadi).
female(manisha).
female(amisha).

male(jatin).
male(dada).
male(amit).
male(shivam).

parent(leena,niyati).
parent(leena,stuti).
parent(jatin,niyati).
parent(jatin,stuti).

parent(dadi,jatin).
parent(dada,jatin).
parent(dada,amit).
parent(dadi,amit).

parent(amit,amisha).
parent(manisha,amisha).
parent(amit,shivam).
parent(manisha,shivam).

father(X,Y):-male(X),parent(X,Y).
mother(X,Y):-female(X),parent(X,Y).

sister(X,Y):-female(X),father(Z,X),father(Z,Y),X\=Y.
brother(X,Y):-male(X),father(Z,X),father(Z,Y),X\=Y.

grandma(X,Y):-father(Z,Y),mother(X,Z).
grandpa(X,Y):-father(Z,Y),father(X,Z).

uncle(X,Y):-father(Z,Y),brother(X,Z).
aunt(X,Y):-uncle(Z,Y),father(Z,Q),mother(X,Q).

nephew(X,Y):-male(X),father(Z,X),brother(Y,Z).
niece(X,Y):-female(X),father(Z,X),brother(Y,Z).
OUTPUT:

?- mother(X,niyati).
X = leena ;
false.

?- brother(XX,YY).
XX = jatin,
YY = amit ;
XX = amit,
YY = jatin ;
XX = shivam,
YY = amisha ;
false.

?- grandma(dadi,QQ).
QQ = niyati ;
QQ = stuti ;
QQ = amisha ;
QQ = shivam ;
false.

?-uncle(QQ,niyati).
QQ = amit ;
false.

?- niece(XX,amit).
XX = niyati ;
XX = stuti ;
false.
EXPERIMENT NO. 2

Write a program that list four addresses in a label form, each address
should list a name, one-line address, city, state & pin-code.

printX :-
write('ABC'),nl,write('E-1-43'),nl,write('Rajkot'),nl,write('Gujarat - '),
write('360001'),nl,nl,

write('DEF'),nl, write('5C-738'),nl,write('Gandhinagar'),nl,write('Gujarat - '),


write('381006'),nl,nl,

write('PQR'),nl,write('C-13'),nl,write('Jamnagar'),nl,write('Gujarat - '),
write('361008'),nl,nl,

write('XYZ'),nl,write('S-54'),nl,write('Ahmedabad'),nl,write('Gujarat - '),
write('380001'),nl,nl.

OUTPUT:

?- printX.
ABC
E-1-43
Rajkot
Gujarat - 360001

DEF
5C-738
Gandhinagar
Gujarat - 381006

PQR
C-13
Jamnagar
Gujarat - 361008

XYZ
S-54
Ahmedabad
Gujarat - 380001

true.
EXPERIMENT NO. 3

Write a program to find roots of quadratic equation. (Consider all possible


cases).

printX :-
write('Enter value of a, b, c :'),nl,
read(A),
read(B),
read(C),
Delta is (B * B) - (4 * A * C),
r_Delta(Delta, A, B).

r_Delta(X,A,B) :-
X=0, ROOT is (-B) / (2 * A),
write('Root = '), write(ROOT), nl;

X>0, SQRT is sqrt(X),


ROOT1 is (-B + SQRT)/(2 * A),
ROOT2 is (-B - SQRT)/(2 * A) ,
write('Root 1 = '), write(ROOT1), nl,
write('Root 2 = '), write(ROOT2);

X<0, write('No Real Roots.').

OUTPUT:

?- printX.
Enter value of a, b, c :
|: 1.
|: 2.
|: 1.
Root = -1
true .

?- printX.
Enter value of a, b, c :
|: 1.
|: 3.
|: 1.
Root 1 = -0.3819660112501051
Root 2 = -2.618033988749895
true .
?- printX.
Enter value of a, b, c :
|: 1.
|: 2.
|: 3.
No Real Roots.
true.
EXPERIMENT NO. 4

Write a program to find minimum & maximum from give Numbers.

maxList([H],H).
maxList([H|T],Max):- Max = H, maxList(T,Max1), H >= Max1.
maxList([H|T],Max):- Max = Max1, maxList(T,Max1),H < Max1.

miniList([H],H).
miniList([H|T],Min):- Min = H, miniList(T,Min1), H < Min1.
miniList([H|T],Min):- Min = Min1, miniList(T,Min1),H >= Min1.

OUTPUT :

?- maxList([3,1,5,2,4],MAX).
MAX = 5 ;
false.

?- miniList([3,1,5,2,4],MIN).
MIN = 1 ;
false.
EXPERIMENT NO. 5

Write a program to solve Water-Jug Problem.

jug(2, _).
jug(0, 2):- write('(0, 2)'), nl,
write('(2, 0)'), nl.
jug(4, 0):- write('(4, 0)'), nl,
jug(0, 0).
jug(4, 3):- write('(4, 3)'), nl,
jug(0, 0).
jug(3, 0):- write('(3, 0)'), nl,
jug(3, 3).
jug(X, 0):- write('('),write(X), write(', 0)'), nl,
jug(0, 3).
jug(0, 3):- write('(0, 3)'), nl,
jug(3, 0).
jug(0, X):- write('(0, '),write(X),write(')'), nl,
jug(0, 0).
jug(3, 3):- write('(3, 3)'), nl,
jug(4, 2).
jug(4, 2):- write('(4, 2)'), nl,
write('(2, 0)'), nl,
jug(2, 0).
jug(X, Y):- X>4, fail,
Y>3, fail.

OUTPUT:

?- jug(4,3).
(4, 3)
(0, 0)
(0, 3)
(3, 0)
(3, 3)
(4, 2)
(2, 0)
true.
EXPERIMENT NO. 6

Write a program to find factorial of given number.

factorial(0,1).
factorial(N,F) :-
N > 0,
N1 is N - 1,
factorial(N1,F1),
F is N * F1.

OUTPUT:

?- factorial(5,Ans).
Ans = 120 ;
false.
EXPERIMENT NO. 7

Write a menu driven program to display set of questions to user and give
answer of selected question.

likes(niyati,apple).
likes(stuti,mango).
likes(shivam,apple).
likes(niyati,milk).
likes(amisha,buttermilk).
likes(stuti,buttermilk).

hobby(niyati,reading).
hobby(stuti,reading).
hobby(stuti,chess).
hobby(amisha,chess).
hobby(shivam,cricket).

instrument(niyati,piano).
instrument(stuti,piano).
instrument(amisha,piano).
instrument(shivam,piano).
instrument(shivam,tabla).

go:-
writeln('Q-1. Who likes apple?'),
writeln('Q-2. Does anybody like apple?'),
writeln('Q-3. Is it true that nobody likes apple?'),
writeln('Q-4. Who likes apple as well as enjoys playing cricket and piano?'),
writeln('Q-5. Does anybody play at least one instrument?'),
writeln('Q-6. Who likes to play chess, drink buttermilk but does not play any instrument?'),
writeln('Q-7. Who share at least one hobby and at least one instrument?'),
writeln('Q-8. Who are the persons sharing common instruments but no hobbies are in
common?'),
write('Enter question number : '),
read(N),ques(N).

ques(1):- likes(X,apple), writeln(X),fail.


ques(2):- likes(_,apple), write('yes');write('no'),!.
ques(3):- not(likes(_,apple)), write('yes');write('no'),!.
ques(4):- likes(X,apple), hobby(X,cricket), instrument(X,piano),
write(X); write('nobody').
ques(5):- instrument(_,_),write('yes');write('no').
ques(6):- hobby(X,chess),likes(X,buttermilk),not(instrument(X,_)),
write(X);write('nobody').
ques(7):- hobby(X,H),hobby(Y,H),instrument(X,I),instrument(Y,I),X\=Y,
write(X),write(' and '),write(Y),nl,fail.
OUTPUT:

?- go.
Q-1. Who likes apple?
Q-2. Does anybody like apple?
Q-3. Is it true that nobody likes apple?
Q-4. Who likes apple as well as enjoys playing cricket and piano?
Q-5. Does anybody play at least one instrument?
Q-6. Who likes to play chess, drink buttermilk but does not play any instrument?
Q-7. Who share at least one hobby and at least one instrument?

Enter question number : 1.


niyati
shivam
false.

Enter question number : 2.


yes
true .

Enter question number : 3.


no
true.

Enter question number : 4.


shivam
true

Enter question number : 5.


yes
true

Enter question number : 6.


nobody
true.

Enter question number : 7.


niyati and stuti
stuti and niyati
stuti and amisha
amisha and stuti
false.
EXPERIMENT NO. 8

Write a PROLOG program (a) To find the length of a list. (b) To sum all
numbers of List.

(a). To find the length of a list.

len([ ],0).
len([H|T],N):- len( T , N1 ),N is 1 + N1.

OUTPUT:

?- len([1,2,3,4,5,6],Length).
Length = 6.

(b). To sum all numbers of list.

sumofdigits([ ],0).
sumofdigits([H|T],N):- sumofdigits(T,N1),
N is H + N1.

OUTPUT:

?- sumofdigits([1,2,3,4],Ans).
Ans = 10.
EXPERIMENT NO. 9

Write a PROLOG program for Arithmetic Operations: -

(a). To check if a given year is a Leap Year or not.

go:- write('Enter an year : '),


read(Y),
leap(Y),
write('Leap year.'),nl.

go:- write('Not leap year.'),nl.

leap(Y):- X is Y mod 4,
X=0;
X is Y mod 400,
X = 0,
X is Y mod 100,
X \= 0.

OUTPUT:

?- go.
Enter an year : 1992.
Leap year.
true.

?- go.
Enter an year : 1993.
Not leap year.
true.

(b). To find the Greatest Common Divisor.

gcd(X,Y):- gcd(X,Y,A),write('GCD of '),


write(X),write(' and '),
write(Y),write(' = '),write(A),nl.
gcd(X,0,X).
gcd(X,Y,A):- Y>0,
R is X mod Y,
gcd(Y,R,A).

OUTPUT:

?- gcd(18,12).
GCD of 18 and 12 = 6
true.
(c). To find the Least Common Multiple.

gcd(X,Y):- gcd(X,Y,A),write('GCD of '),


write(X),write(' and '),
write(Y),write(' = '),write(A),nl.
gcd(X,0,X).
gcd(X,Y,A):- Y>0,
R is X mod Y,
gcd(Y,R,A).

lcm(X,Y):- lcm(X,Y,A),write('LCM of '),


write(X),write(' and '),
write(Y),write(' = '),
write(A),nl.
lcm(X,Y,A):- gcd(X,Y,Z),
A is X * Y / Z.

OUTPUT:

?- lcm(18,12).
LCM of 18 and 12 = 36
true.

(d). To generate the Fibonacci series of a given number.

go:- write('Enter a no : '),


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),write(' '),
NN is N - 1,
fibo(AA,BB,CC,NN).

OUTPUT:

?- go.
Enter a no : 7.
1 1 2 3 5 8 13
true.
EXPERIMENT NO. 10

Write a Prolog program to implement Tower Of Hanoi Problem.

hanoi:- hanoi(5).
hanoi(N):- moveit(N,1,2,3).

moveit(1,X,Y,_) :-
write('Move top disk from '),
write(X), write(' to '),
write(Y), nl.
moveit(N,X,Y,Z) :-
N > 1,
M is N-1,
moveit(M,X,Z,Y),
moveit(1,X,Y,_),
moveit(M,Z,Y,X).

OUTPUT :

?- hanoi(3).
Move top disk from 1 to 2
Move top disk from 1 to 3
Move top disk from 2 to 3
Move top disk from 1 to 2
Move top disk from 3 to 1
Move top disk from 3 to 2
Move top disk from 1 to 2
true.
EXPERIMENT NO. 11

Write a PROLOG program for finding the Net salary of an employee.

emp:-
write('Enter Employee Name : '),
read(Name),
write('Enter Department name : '),
read(Dept),
write('Enter Basic : '),
read(Basic),
write('Enter Dearness_pay : '),
read(DP),
write('Enter HRA : '),
read(HRA),
write('Enter Overtime : '),
read(OT),
write('Enter CPG : '),
read(CPG),
write('Enter Income Tax : '),
read(IT),
Salary is Basic + DP + HRA + OT - CPG - IT,
write('Name = '), write(Name),nl,
write('Department = '), write(Dept),nl,
write('Net Salary = '), write(Salary),nl.

OUTPUT:

?- emp.
Enter Employee Name : abc .
Enter Department name : ce .
Enter Basic : 50000.
Enter Dearness_pay : 1000.
Enter HRA : 8000.
Enter Overtime : 3000.
Enter CPG : 1500.
Enter Income Tax : 3000.

Name = abc
Department = ce
Net Salary = 57500
true.
EXPERIMENT NO. 12

Write a Prolog program to demonstrate the effective use of Cut and Fail.

person(khushbu).
person(niyati).
person(misba).

hobby(reading).
hobby(tv).
hobby(music).
hobby(dance).

has_hobby(khushbu,music).
has_hobby(khushbu,dance).

has_hobby(niyati,reading).
has_hobby(niyati,tv).

has_hobby(misba,tv).
has_hobby(misba,music).
has_hobby(misba,reading).

% to show all hobbies of first person


everybody:- person(X),!, has_hobby(X,Y), write(X),write(' has hobby '),write(Y),nl,fail.

% to show one hobby of first person


everybody1:- person(X), has_hobby(X,Y),!, write(X),write(' has hobby '),write(Y),nl.

% to show only one hobby of each person


everybody2:- person(X), \+ (has_hobby(X,Y), write(X),write(' has hobby '),write(Y),nl).

OUTPUT:

?- everybody.
khushbu has hobby music
khushbu has hobby dance
false.

?- everybody1.
khushbu has hobby music
true.

?- everybody2.
khushbu has hobby music
niyati has hobby reading
misba has hobby tv
false.

You might also like