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

Ai - Lab - Assignment - SD

The document is a compilation of Prolog programming exercises for an Artificial Intelligence Lab course at JIS College of Engineering. It includes 29 different programming tasks, such as finding the maximum of three numbers, calculating factorials, generating Fibonacci series, and implementing sorting algorithms. Each task is accompanied by a Prolog code solution demonstrating the required functionality.

Uploaded by

seothi1701
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)
6 views16 pages

Ai - Lab - Assignment - SD

The document is a compilation of Prolog programming exercises for an Artificial Intelligence Lab course at JIS College of Engineering. It includes 29 different programming tasks, such as finding the maximum of three numbers, calculating factorials, generating Fibonacci series, and implementing sorting algorithms. Each task is accompanied by a Prolog code solution demonstrating the required functionality.

Uploaded by

seothi1701
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/ 16

JIS College of Engineering

Department of Computer Science and Engineering


Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

Name Sayan Das

University Roll 123190803085


Number
Section B

Group 1

Mobile Number 6296778033

Email Id [email protected]
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

List of Experiments: (Artificial Intelligence Lab (CS791A))

1. Write a program in PROLOG to find the maximum of three


numbers.
Ans-

max(integer,integer,integer)

clauses

max(A,B,C):-

A>B,

A>C,

write(A).

max(A,B,C):-

A>B,

write(C).

max(_,B,C):-

B>C,

write(B).

max(_,_,C):-

write(C).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
2. Write a program in PROLOG to calculate the factorial of a number.
Ans-
fact(0,1).
fact(N,F):-
(

% The below is for +ve factorial.


N>0 ->
(
N1 is N-1,
fact(N1,F1),
F is N*F1
)
;

% The below is for -ve factorial.


N<0 ->
(
N1 is N+1,
fact(N1,F1),
F is N*F1
)
).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

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


Ans-
gcd(X,0,X).

gcd(X,Y,Z):-

R is mod(X,Y),

gcd(Y,R,Z).

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


Ans-
fib(0, 1) :- !.
fib(1, 1) :- !.
fib(N, F) :-
N > 1,
N1 is N-1,
N2 is N-2,
fib(N1, F1),
fib(N2, F2),
F is F1+F2.
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

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


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

6. Write a program in PROLOG to insert an element at the beginning/


middle/ end of a list.
Ans-
At First Position-
ins_first(Item,List,[Item|List]).

At End Position-
ins_end(X,Y,Z):-
append(Y,[X],Z).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
7. Write a program in PROLOG to find the GCD of the elements of a
list.
Ans-
gcd(0,X,X):- X > 0, !.
gcd(X,Y,Z):- X>=Y, X1 is X -Y, gcd(X1,Y,Z).
gcd(X,Y,Z):- X<Y, X1 is Y-X, gcd(X1,X,Z).
gcdL([H,H1|T],Z):-gcd(H,H1,X),gcdL([X|T],Z).
gcdL([H1,H2],Z):-gcd(H1,H2,Z).

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


Ans-
list_max([P|T], O) :- list_max(T, P, O).

list_max([], P, P).
list_max([H|T], P, O) :-
( H>P
-> list_max(T, H, O)
; list_max(T, P, O)).

9. Write a program in PROLOG to reverse a list


Ans-
list_concat([],L,L).
list_concat([X1|L1],L2,[X1|L3]) :- list_concat(L1,L2,L3).

list_rev([],[]).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
list_rev([Head|Tail],Reversed) :-
list_rev(Tail, RevTail),list_concat(RevTail, [Head],Reversed).

10. Write a program in PROLOG to check whether a number or string is


a palindrome or not.
Ans-
palindrome(Xs):- palindrome(Xs,[]).

palindrome(Xs, Xs):- !. % Don't need to redo after positive match


palindrome([X|Xs],Xs):- !.
palindrome([X|Xs],Ys):- palindrome(Xs, [X|Ys]).

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


Ans-
list_delete(X, [X], []).

list_delete(X,[X|L1], L1).

list_delete(X, [Y|L2], [Y|L1]) :- list_delete(X,L2,L1).


JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
12. Write a program in PROLOG for linear search/ binary search in a list.
Ans-
contains(List, Value):- even_division(_, [Value|_], List).
contains(List, Value):- even_division(_, [Center|SecondHalf], List),
Center<Value, SecondHalf \= [],
contains(SecondHalf, Value).

contains(List, Value):- even_division(FirstHalf, [Center|_], List),


Center>Value, FirstHalf\=[],
contains(FirstHalf, Value).
even_division(First, Second, Xs) :- append(First, Second, Xs),
length(First,F), length(Second,S),
S>=F, S-F=<1.

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


sort algorithm.
Ans-
bubblesort([], []).
bubblesort([H], [H]).
bubblesort([H|D], R) :-
bubblesort(D, E),
[B|G] = E,
( (H =< B, R = [H|E])
; (H > B, bubblesort([B,H|G], R))
).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

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


Ans-
move(1,X,Y,_) :-

write('Move top 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).

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


Ans-
solution(Queens) :-
permutation([1,2,3,4,5,6,7,8], Queens),
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
safe(Queens).

% safe(Queens): Queens is a list of Y-coordinates of non-attacking queens

safe([]).

safe([Queen|Others]) :-
safe(Others),
noattack(Queen,Others,1).

% noattack(Queen, Queens, Dist):


% Queen does not attack any queen in Queens at horizontal distance Dist

noattack(_,[],_).
noattack(Y,[Y1|Ylist],Xdist) :-
Y1 - Y =\= Xdist, % Not upward diagonal attack
Y - Y1 =\= Xdist, % Not downward diagonal attack
Dist1 is Xdist + 1,
noattack(Y,Ylist,Dist1).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
16 Write a Prolog Program to find the sum of inverse of N numbers.
Ans-
sumofinverse(N,R) :-
sumofinverse(N,0,R).
sumofinverse(0,R,R).
sumofinverse(N,T,R) :- N > 0 , T1 is T+(1/N), N1 is N-1,
sumofinverse(N1,T1,R).

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


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

18 Write a Prolog Program to find the square root of a number using


function.
Ans-
squareroot(X):-A is sqrt(X),write(A).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
19 Write a Prolog Program to find the modulus of two numbers using
function.
Ans-
modulo(A,B):-X is A mod B , write(X).

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


Ans-
check(X,Res) :- X>0, !, Res='Positive';
X<0, !, Res='Negative';
Res='Zero'.

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


Ans-
even(0).
even(X) :- X > 0, X1 is X - 1, odd(X1).
even(X) :- X < 0, X1 is X + 1, odd(X1).
odd(1).
odd(X) :- X > 0, X1 is X - 1, even(X1).
odd(X) :- X < 0, X1 is X + 1, even(X1).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
22 Write a Prolog Program to print N to 20 using for loop (Fixing the end
value).
Ans-
print_numbers(20) :- write(20), !.
print_numbers(X) :- write(X), nl, Next is X + 1, print_numbers(Next).

23 Write a Prolog Program to print N to 20 numbers using for loop, in


reverse order.
Ans-
print_numbers(0) :- write(0), !.
print_numbers(X) :- write(X), nl, Next is X - 1, print_numbers(Next).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A

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


Ans-
printloop(F, L) :- F =\= L, write(F), nl, N is F+1, printloop(N,L).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
25 Write a Prolog Program to find whether the number is present in list or
not.
Ans-
list_member(X,[X|_]).
list_member(X,[_|TAIL]) :- list_member(X,TAIL).

26 Write a Prolog Program to find the length of the list.


Ans-
list_length([a,b,c,d,e,f,g,h,i,j],Len).

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


Ans-
prod_list([],0).
prod_list([H],H).
prod_list([H|T], Product) :- prod_list(T, Rest), Product is H * Rest.

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


Ans-
list_member(X,[X|_]).
list_member(X,[_|TAIL]) :- list_member(X,TAIL).
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Artificial Intelligence Lab
Paper Code: CS 701A
29 Write a Prolog Program to find sum of all list members.
Ans-
sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, Rest),
Sum is H + Rest.

You might also like