0% found this document useful (0 votes)
48 views8 pages

Artificial Intelligence Lab

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)
48 views8 pages

Artificial Intelligence Lab

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/ 8

Artificial Intelligence Lab

PART 1

1) Write a program to find the maximum of three numbers

max(A, B, C) :- (A>B;A=B),(A>C;A=C), write(A).


max(A, B, C) :- (A<B;A=B),(B>C;B=C), write(B).
max(A, B, C) :- (C>B;C=B),(A<C;A=C), write(C).

2) Write a Prolog Program to find the factorial of a number.

factorial(0, 1).
factorial(N, M) :- N > 0,
Prev is N -1,
factorial(Prev, M1),
M is M1 * N.

3) Write a program in PROLOG to calculate the GCD of two numbers

gcd(X,0):- write(X).
gcd(X,Y):-
M is mod(X,Y),
gcd(Y,M).

1|Page
Artificial Intelligence Lab

4) Write a program in PROLOG to generate the Fibonacci series

fib(0,0).
fib(1,1).
fib(F,N) :-
N>1,
N1 is N-1,
N2 is N-2,
fib(F1,N1),
fib(F2,N2),
F is F1+F2.

5) Write a program in PROLOG to count the number of elements in a list.

count(0,[]).
count(N,[_|TAIL]) :-
count(N1,TAIL),
N is N1+1.

6) Write a program to insert an element at the beginning/ middle/ end of a list


insert_begin(X,L,R) :-
R = [X|L].
insert_end(X,[],[X]).
insert_end(X,[Y|TAIL],[Y|R]) :-
insert_end(X,TAIL,R).
insert_mid(X,0,Y,[X|Y]).
insert_mid(X,POS,[Y|TAIL],[Y|R]) :-
P is POS-1,
insert_mid(X,P,TAIL,R).

2|Page
Artificial Intelligence Lab

7) Write a program in PROLOG to find the GCD of the elements of a list


gcd(X,0,X).
gcd(X,Y,R):-
M is mod(X,Y),
gcd(Y,M,R).

gcdList(X,[],X).
gcdList(X,[Y|TAIL],R) :-
gcd(X,Y,Z),
gcdList(Z,TAIL,R).
find_gcd(X,[A,B|TAIL]):-
gcd(A,B,R),
gcdList(R,TAIL,X).

8) Write a program in PROLOG to find the maximum of a list


max(A,B,X):-
A > B, X is A.
max(A,B,X):-
B >= A, X is B.
maxList(X,[],X).
maxList(X,[Y|TAIL],R) :-
max(X,Y,Z),
maxList(Z,TAIL,R).
find_max(X,[A,B|TAIL]):-
max(A,B,R),
maxList(R,TAIL,X).

9) Write a program in PROLOG to reverse a list


insert_end(X,[],[X]).
insert_end(X,[Y|TAIL],[Y|R]) :-
insert_end(X,TAIL,R).
revList([],[]).
revList([Y|TAIL],Z) :-
revList(TAIL,R),
insert_end(Y,R,Z).

3|Page
Artificial Intelligence Lab

10) Write a program in PROLOG to check whether a number or string is a palindrome or not
insert_end(X,[],[X]).
insert_end(X,[Y|TAIL],[Y|R]) :-
insert_end(X,TAIL,R).
revList([],[]).
revList([Y|TAIL],Z) :-
revList(TAIL,R),
insert_end(Y,R,Z).
palin(X):- revList(X,X),write('Palindrome').

11) Write a program in PROLOG to delete an element from a list


deleteL(X,[X|TAIL],TAIL).
deleteL(X,[Y|TAIL],[Y|R]) :-
deleteL(X,TAIL,R).

12) Write a program in PROLOG for linear search/ binary search in a list
linSearch(X,[X|TAIL],1).
linSearch(X,[Y|TAIL],POS):-
linSearch(X,TAIL,P),
POS is P+1.

13) Write a program in PROLOG to sort n numbers using the bubble sort algorithm .

bubble_sort(List,Sorted):-b_sort(List,[],Sorted).
b_sort([],Acc,Acc).
b_sort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),b_sort(NT,[Max|Acc],Sorted).
bubble(X,[],[],X).
bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).

4|Page
Artificial Intelligence Lab

14) Write a program in PROLOG for Towers of Hanoi problem


toh(1,X,Y,_) :-
write(X), write(' to '), write(Y), nl.
toh(N,X,Y,Z) :-
N>1,
M is N-1,
toh(M,X,Z,Y),
toh(1,X,Y,_),
toh(M,Z,Y,X).

15) Write a program in PROLOG for 4-Queens problem


use_module(library(lists)).
n_queen(N, Solution) :-
length(Solution, N),
queen(Solution, N).
up2N(N,N,[N]) :-!.
up2N(K,N,[K|Tail]) :- K < N, K1 is K+1, up2N(K1, N, Tail).
queen([],_).
queen([Q|Qlist],N) :-
queen(Qlist, N),
up2N(1,N,Candidate_positions_for_queenQ),
member(Q, Candidate_positions_for_queenQ),
check_solution(Q,Qlist, 1).
check_solution(_,[], _).
check_solution(Q,[Q1|Qlist],Xdist) :-
Q =\= Q1,
Test is abs(Q1-Q),
Test =\= Xdist,
Xdist1 is Xdist + 1,
check_solution(Q,Qlist,Xdist1).

5|Page
Artificial Intelligence Lab

PART 2

2. Write a Prolog Program to find the sum of square of N numbers.


sumSq(1,1).
sumSq(N,R):-
N > 1,
N1 is N-1,
sumSq(N1,R1),
P is N*N,
R is R1+P.

3. Write a Prolog Program to find the sum of inverse of N numbers.


suminv(N,R) :-
suminv(N,0,R).
suminv(0,R,R).
suminv(N,T,R) :- N > 0 , T1 is T+(1/N), N1 is N-1,
suminv(N1,T1,R).

4. Write a Prolog Program to find sum of first N natural numbers.


sumN(0,0).
sumN(N,R):-
N > 0,
N1 is N-1,
sumN(N1,R1),
R is R1+N.

6. Write a Prolog Program to find the square root of a number using function.
sqRoot(X,R):- R is sqrt(X).

7. Write a Prolog Program to find the modulus of two numbers using function.
modulus(X,Y):-A is mod(X,Y),write(A).

9. Write a Prolog Program to find the positive, negative or zero.


check(A):-A>0,write('Positive');
A<0,write('Negative');
write('Zero').

6|Page
Artificial Intelligence Lab

10. Write a Prolog Program to find even odd using function.


check(A):-X is A mod 2,
(X is 0,write('Even'));
write('Odd').

11. Write a Prolog Program to print N to 20 using for loop (Fixing the end value).
fun(20):-write(20).
fun(N):-N < 20,
write(N),write(', '),
N1 is N+1,
fun(N1).

12. Write a Prolog Program to print N to 20 numbers using for loop, in reverse order.
fun(20):-write(20).
fun(N):-N > 20,
write(N),write(', '),
N1 is N-1,
fun(N1).

13. Write a Prolog Program to print N to M numbers using for loop.


fun(N,N):-write(N).
fun(M,N):-M < N,
write(M),write(', '),
M1 is M+1,
fun(M1,N).

17. Write a Prolog Program to find multiplication of all list members


multL([X],X).
multL([X|TAIL],R):-
multL(TAIL,R1),
R is R1*X.

18. Write a Prolog Program to append a new list to another list.


appendL([], L, L).
appendL([H|TAIL], L, [H|R]) :-
appendL(TAIL, L, R).

7|Page
Artificial Intelligence Lab

19. Write a Prolog Program to find sum of all list members.


list_sum([],0).
list_sum([Head|Tail], Sum) :-
list_sum(Tail,SumTemp),
Sum is Head + SumTemp.

20. Write a Lisp program to find whether a number is positive or negative or zero.
(setq a (read))
(cond
( (> a 0) (format t "~d is positive" a) )
( (< a 0)(format t "~d is negative" a) )
(t (format t "~d is zero" a))
)

21. Write a Lisp program to find area of circle.


(princ "Enter Radius: ")
(defvar radius (read))
(defvar area(* 3.14 radius radius ))
(princ radius)
(terpri)
(princ "Area: ")
(write area)

22. Write a Lisp program to print N natural numbers.


(princ "Enter number: ")
(defvar n (read))
(loop for a from 1 to n do (princ a)(princ " ") )

23. Write a Lisp program to add function to add two numbers.


(defun addfunc (n1 n2)
( + n1 n2 )
)
(princ "Enter first number: ")
(defvar n1 (read))
(princ "Enter second number: ")
(defvar n2 (read))
(write(addfunc n1 n2))

8|Page

You might also like