Ai Lab Manual
Ai Lab Manual
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('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
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;
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
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
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
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).
?- 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?
Write a PROLOG program (a) To find the length of a list. (b) To sum all
numbers of 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.
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
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.
OUTPUT:
?- gcd(18,12).
GCD of 18 and 12 = 6
true.
(c). To find the Least Common Multiple.
OUTPUT:
?- lcm(18,12).
LCM of 18 and 12 = 36
true.
OUTPUT:
?- go.
Enter a no : 7.
1 1 2 3 5 8 13
true.
EXPERIMENT NO. 10
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
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).
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.