0% found this document useful (0 votes)
19 views

Ai 9

The document contains 13 Prolog programs covering topics like factorial, Fibonacci series, membership, reversing lists, checking palindromes, arithmetic operations, loops, finding length of lists, and implementing a block and table structure.

Uploaded by

Arnab Banerjee
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)
19 views

Ai 9

The document contains 13 Prolog programs covering topics like factorial, Fibonacci series, membership, reversing lists, checking palindromes, arithmetic operations, loops, finding length of lists, and implementing a block and table structure.

Uploaded by

Arnab Banerjee
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/ 6

PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY

DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING


ASSIGNMENT-9
1. Write a program in prolog to find the Factorial of a given number ‘N’.
Knowledge Representation
factorial(0,1).
factorial(N,F):-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N*F1.
Query and Output
1 ?- factorial(6,W),write(W).
720
W = 720
Yes

2. Write a program in prolog for finding Fibonacci series of nth number.


Knowledge Representation
%1-terminating
fibonacci(1,0).
%2-terminating
fibonacci(2,1).
%3-doubly recursive
fibonacci(Numb,Fib):-
Numb>2,
Numb1 is Numb-1,
Numb2 is Numb-2,
fibonacci(Numb1,Fib1),
fibonacci(Numb2,Fib2),
Fib is Fib1+Fib2.
Query and Output
2 ?- fibonacci(7,D),write(D).
8
D=8
Yes

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -
PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY
DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING

3. Write a program in prolog for the membership.


Knowledge Representation
member(X,[X|_]).
member(X,[_|T]):- member(X,T).
Query and Output
1 ?- member(b,[a,v,b,c]).
Yes
2 ?- member(X,[a,b,c]),write(X),nl,fail.
a
b
c
No

4. Write a program in prolog to find the reverse of a list.


Knowledge Representation
reverse([X | Y],Z,W):-reverse(Y,[X | Z],W).
reverse([],X,X).
Query and Output
4 ?- reverse([1,2,3],[],A).
A = [3, 2, 1]
Yes

5. Write a program in prolog to find whether a given word is palindrome or not.


Knowledge Representation
palindrome(L):- reverse(L,L).
my_reverse([],[]).
my_reverse([H|T],R):- my_reverse(T,T1),append(T1,[H],R).

Query and Output


1 ?- palindrome([m,a,d,a,m]).
Yes
2 ?- palindrome([t,e,a,c,h,e,r]).
No

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -
PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY
DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING

6. Write a program in prolog to find the sum of two numbers.


Knowledge Representation
start:-sum.
sum:-
write('X='),read(X),
write('Y='),read(Y),
S is X+Y,
write('Sum is '),write(S).
Query and Output
6 ?- start.
X=9.
Y=14.
Sum is 23
Yes

7. Write a program in prolog to find the subtraction of two numbers.


Knowledge Representation
start:-sub.
sub:-
write('X='),read(X),
write('Y='),read(Y),
S is X-Y,
write('Difference is '),write(S).
Query and Output
7 ?- start.
X=35.
Y=14.
Difference is 21
Yes

8. Write a program in prolog to find the product of two numbers.


Knowledge Representation
start:-mul.
mul:-
write('X='),read(X),
write('Y='),read(Y),
M is X*Y,
write('Product is '),write(M).
Query and Output
8 ?- start.
X=7.
Y=4.
Product is 28
Yes

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -
PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY
DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING

9. Write a program in prolog to find the result after division of two numbers.
Knowledge Representation
start:-div.
div:-
write('X='),read(X),
write('Y='),read(Y),
D is X/Y,
write('Result is '),write(D).
Query and Output
9 ?- start.
X=5.
Y=3.
Result is 1.66667
Yes

10. Write a program in prolog to find the summation of arithmetic progression.


Knowledge Representation
start:-ap.
ap:-write('N='),read(N),
write('A='),read(A),
write('D='),read(D),
S is ((N/2)*(A+((N-1)*D))),
write('A.P. sum is '),write(S).
Query and Output
10 ?- start.
N=4.
A=2.
D=2.
A.P. sum is 16
Yes

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -
PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY
DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING

11. Write a program in prolog to create a loop for n numbers.


Knowledge Representation
loop(0).
loop(N):-N>0,write(' The value is: '),write(N),nl,
M is N-1,loop(M).
Query and Output
11 ?- loop(9).
The value is: 9
The value is: 8
The value is: 7
The value is: 6
The value is: 5
The value is: 4
The value is: 3
The value is: 2
The value is: 1
Yes

12. Write a program in prolog to find the length of a list.


Knowledge Representation
size([],0).
size([H|T],N) :- size(T,N1), N is N1+1.
Query and Output
1 ?- size([1,2,3,4],N).
N=4
Yes
2 ?- size([bill,ted,ming,pascal,nat,ron],N).
N=6
Yes
3 ?- size([a, [b, c, d], e, [f | g], h], N).
N=5
Yes

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -
PAILAN COLLEGE OF MANAGEMENT AND TECHNOLOGY
DEPARTMENT: - COMPUTER SCIENCE AND ENGINEERING

13. Write a program in prolog to implement BLOCK AND TABLE.


Knowledge Representation
block(block1).
block(block2).
block(block3).
block(block4).
table(table1).
on(block1,block2).
on(block2,table1).
on(block3,block4).
on(block4,table1).
above(X,Y):-block(X),block(Y),on(X,Y).
above(X,Y):-block(X),table(Y),on(X,Y).
above(X,Y):-block(X),block(Z),on(X,Z),above(Z,Y).
Query and Output

1 ?- on(block1, table1).
No
2 ?- above(block1, table1).
Yes
3 ?- above(block1, block2).
Yes
4 ?- above(block2, X).
X = table1
Yes
5 ?- above(block3, X).
X = block4
Yes
6 ?- above(X, table1).
X = block2
Yes
7 ?- above(X,Y).
X = block1
Y = block2
Yes

Figure of BLOCK AND TABLE Problem

1 3
2 4
__________________________________________________________

NAME: - AVIRUP NATH ROLL NO: - 42


TEACHER’S SIGNATURE: - PAGE NO: -

You might also like