You are on page 1of 19

1.Write a prolog program to calculate the sum of two numbers.

sum:- write("\nEnter the first number

: "), read(X), write("\nEnter the

second number : "), read(Y), S is X+Y,

write("\nSum : "), write(S).

2. Write a program to implement max(X,Y,M) so that M is maximum of two numbers X and Y.

max:- write("\nEnter the first

number : "), read(X), write("Enter

the second number : "), read(Y),

max(X,Y,M).

max(X,Y,M):-

X>Y,

M is X, write("\nMaximum

number is "), write(M).

max(X,Y,M):-

X<Y,

M is Y, write("\nMaximum

number is "), write(M).


max(X,Y,M):-

X=Y,

M is X, write("\nBoth the numbers are

equal").

3. Write a program in PROLOG to implement factorial (N, F) where F represents the factorial of a
number N.

factorial:- write("\nEnter the

number : "), read(N),

F is 1, fact(N,F).

fact(N,F):- N>=1,

F1 is N*F, M

is N-1,

fact(M,F1).

fact(0,F):- write("\nFactorial of the given

number : "), write(F).


4. Write a Prolog program to implement Fibonacci(N,T) where T is the Nth term of the Fibonacci
series.

fib:- write("\nEnter the term (greater than 0

) : "), read(N), fibonacci(N,T), write(T).

fibonacci(N,T):-

N>=3,

Y is N-1, Z is

N-2,

fibonacci(Y,P),

fibonacci(Z,Q),

T is P + Q.

fibonacci(1,1). fibonacci(2,1).
5. Write a Prolog program to implement GCD of two numbers.

start:- write("\nEnter the first

number : "), read(X), write("Enter

the second number : "), read(Y),

write("\nGCD is "), gcd(X,Y).

gcd(0,X):- write(X).

gcd(X,0):- write(X).

gcd(X,Y):- X

>= Y, G is X

mod Y,

gcd(G,X).

gcd(X,Y):- X <

Y, G is Y mod

X, gcd(X,G).
6. Write a prolog program to implement power(N,P,A) where N is raised to the power P to get A.

power:- write("\n Enter the number : "),

read(N), write("\n Enter the power (positive

number): "), read(P), pow(N,P,A),

write("\nResult : "), write(A).

pow(N,0,1). pow(0,P,0).

pow(N,1,N).

pow(N,P,A):- P>0,

P1 is P-1,
pow(N,P1,A1),

A is N*A1.
7. Write a prolog program to implement muti(N,M,R) where N and M are the numbers to be

multiplied and R is the result. multiply:- write("\nEnter the first number : "), read(X), write("\nEnter

the second number : "), read(Y), multi(X,Y,R), write(R).

multi(X,Y,R):-

R is X*Y.

8. Write a program in PROLOG to implement towerofhanoi (N) where N represents the number

of Discs move(1,X,Y,_):- write("\n move disk from: "), write(X), write(" to "), write(Y).
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).

9. Consider a cyclic directed graph [edge (p, q), edge (q, r), edge (q, r), edge (q, s), edge (s,t)] where

edge (A,B) is a predicate indicating directed edge in a graph from a node A to a node B. Write a

program to check whether there is a route from one node to another node. edge(p,q). edge(q,r).

edge(p,r). edge(q,s). edge(s,t).

member(X,[X|_]). member(X,[_|Y]):-

member(X,Y).

append([],L2,L2).

append([H|L1],L2,[H|Result]):- append(L1,L2,Result). reverse([],[]).

reverse([H|L1],L2):- reverse(L1,L3), append(L3,[H],L2).

connected(X,Y):- edge(X,Y);edge(Y,X).

path(A,B,Path):- travel(A,B,[A],Q),

reverse(Q,Path).
travel(A,B,P,[B|P]):-connected(A,B).

travel(A,B,Visited,Path):-connected(A,C),

C \= B,

\+member(C,Visited),

travel(C,B,[C|Visited],Path).

10. Write a Prolog program to implement memb(X, L): to check whether X is a member of L or not.

start:- write("Enter the list : "), read(L),

write("\nEnter the element to be check :

"), read(X), member(X,L).

member(X,[X|_]). member(X,[_|Y]):-

member(X,Y).
11. Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with
L1 to get the resulted list L3.

start:- write("Enter the elements of the first list

in[]: "), read(List1), write("Enter the elements

of the second list : "), read(List2),

conc(List1,List2,Result),

write("\nResult of the concatenation : "),

write(Result).

conc([],List2,List2).

conc([H|List1],List2,[H|Result]):-

conc(List1,List2,Result).
12. Write a Prolog program to implement reverse (L, R) where List L is original and List R is
reversed list.

start:- write("Enter the elements in the

list: "), read(L1), reverse(L1,L2),

write("\nReversed list is : "),

write(L2). reverse([],[]).

reverse([H|L1],L2):-

reverse(L1,L3),

append(L3,[H],L2).

13. Write a program in PROLOG to implement palindrome (L) which checks whether a list L is
a palindrome or not.

start:- write("Enter the elements in the list

in[]:"), read(L), palindrome(L).

palindrome(L):-

reverse(L,L2),

compare(L,L2). reverse([],[]).

reverse([H|L],L2):-

reverse(L,L3),

append(L3,[H],L2).

compare([X|L],[X|L2]):-

compare(L,L2).

compare([X|L],[Y|L2]):- write("\nEntered

list is not a Palindrome").


compare([],[]):-
write("\nEntered list is a palindrome").

14. Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.

start:- write("Enter the list :

"), read(L), sumlist(L,S),

write("\nSum of the list : "),

write(S).

sumlist([],0).

sumlist([X|Tail],Sum):-

sumlist(Tail,Temp), Sum

is X+Temp.

15. Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so
that they are true if their argument is a list of even or odd length respectively
start:- write("Enter the elements of the

list : "), read(L), length(L,R),

even_odd(L).

length([],0). length([_|T],R):-

length(T,R1),

R is R1+1.

even_odd(L):- length(L,R),

write("\nLength of the list : "),

write(R), write("\n\nThe length of

the list is "),

(R mod 2 =:= 0 ->write(" ~~ EVEN ~~ "); write(" ~~ ODD ~~ ")).

16. Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L
is a list and X represents the Nth element of L.
start:- write("Enter elements in the list starting from 0th

position : "), read(L), write("Enter the position : "), read(N),

n_element(N,X,L), write("The "),write(N),write("th position

element is : "),write(X).

n_element(0,X,[X|_]).

n_element(N,X,[_|Xs]):-
N>0,

N1 is N-1, n_element(N1,X,Xs).

17. Write a program in PROLOG to implement remove_dup (L, R) where L denotes the list with
some duplicates and the list R denotes the list with duplicates removed.

start :- write('Enter the elements of the list :- '),read(L),


remove_dup(L,R),write('The New List is : '),write(R).
remove_dup([],[]). remove_dup([H],[H]). remove_dup([H,H |
T],List) :- remove_dup([H|T],List). remove_dup([H,Y |
T],[H|T1]) :- Y \= H,remove_dup([Y|T],T1).
18. Write a Prolog program to implement maxlist(L, M) so that M is the maximum number in
the list
start :- write('Enter the elements of the list
in [] :- '), read(L),
M is 0,

max_list(L,M).
max_list([H|L],M) :- M>=H,max_list(L,M). max_list([H|L],M) :- M<H,M1
is H,max_list(L,M1). max_list([],M) :- write('The maximum element in
the list is :- '),write(M).

19. Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I into Nth
position of list L to generate a list R.

start :- write('Enter the elements of the list : '), read(L),


write('Enter the element to be inserted : '), read(I), write('Enter
the position where the element is to be inserted : '), read(N),
insert_nth(I,N,L,R), write('The new list is as follows : '), write(R).

insert_nth(I,1,L,[I|L]).
insert_nth(I,N,[H|L],[H|R]) :-
N>1, N1 is N-1,
insert_nth(I,N1,L,R).
20. Write a Program in PROLOG to implement sublist(S, L) that checks whether the list S is the
sublist of list L or not. (Check for sequence or the part in the same order).

sublist( [], _ ). sublist( [X|XS], [X|XSS] ) :-


sublist( XS, XSS ). sublist( [X|XS], [_|XSS] ) :-
sublist( [X|XS], XSS ).

21. Write a Prolog program to implement delete_nth(N, L, R) that removes the element on Nth
position from a list L to generate a list R.

start:- write("Enter the elements in the list in []: "), read(List),


write("Enter the position from which element is to be removed:
"), read(N), delete_nth(List,N,Result), write(Result).
delete_nth(List,N,Result):-delete_nth(List,N,1,Result).
delete_nth([],_,_,[]). delete_nth([_|T],N,N,T1):-!,delete_nth(T,N,1,T1).
delete_nth([H|T],N,C,[H|T1]):-C<N, C1 is C+1,delete_nth(T,N,C1,T1).

22. Write a program in PROLOG to implement delete_all (X, L, R) where X denotes the element
whose all occurrences has to be deleted from list L to obtain list R.

start:-
write("Enter the list : "), read(L),
write("Enter the no to be deleted : "),
read(X), delall(X,L,R), write(R).

delall(X, [], []) :- !. delall(X, [X|Xs], Y) :- !, delall(X, Xs, Y).


delall(X, [T|Xs], Y) :- !,delall(X, Xs, Y2), append([T], Y2, Y).

23. Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first ordered list and
L2 is second ordered list and L3 represents the merged list.
start :- write('Enter the first ordered list in [] :- ') , read(L1) ,

write('Enter the second ordered list in []:- ') , read(L2) ,


merge(L1,L2,L3) , write('The merged ordered list is : ') ,
write(L3).

merge([] , [] , []).
merge([X|T1] , [] , [X|T]) :- merge(T1,[],T). merge([]
, [Y|T2] , [Y|T]) :- merge([],T2,T).
merge([X|T1],[Y|T2],[X|T]) :- X=<Y , merge(T1,[Y|T2],T).
merge([X|T1],[Y|T2],[Y|T]) :- X>=Y , merge([X|T1],T2,T).

24. Write a PROLOG program that will take grammar rules in the following format: NT ->NT | T)*
Where NT is any nonterminal, T is any terminal and Kleene star (*) signifies any number of
repetitions, and generate the corresponding top-down parser, that is:
sentence->noun-phrase, verb-phrase determiner
->[the]
will generate the following: sentence (I, O) :- noun-phrase(I,R), verb-
phrase (R,O).
determiner ([the|X], X) :- !.

s1-->np,vp. np--
>pn. np-->d,n,rel.
vp-->tv,np.
vp-->iv. rel-->[].
rel-->rpn,vp.
pn-->[PN],{pn(PN)}.
pn(mary). pn(henry).
rpn-->[RPN],{rpn(RPN)}.
rpn(that).
rpn(which).
rpn(who). iv--
>[IV],{iv(IV)}.
iv(runs). iv(sits). d-
->[DET],{d(DET)}.
d(a). d(the).
n--
>[N],{n(N)}.
n(book).
n(girl).
n(boy).
tv-->[TV],{tv(TV)}.
tv(gives). tv(reads).

25. Write a prolog program that implements Semantic Networks (ATN/RTN).


cat(tom). cat(cat1).
mat(mat1).
sat_on(cat1,mat1).
bird(bird1). caught(tom,bird1).
like(X,cream) :– cat(X). mammal(X)
:– cat(X).
has(X,fur) :– mammal(X).
animal(X) :– mammal(X). animal(X)
:– bird(X). owns(john,tom).
is_coloured(tom,ginger).
is_a(mat1,mats).
is_a(cat1,cats). is_a(tom,cats).
is_a(bird1,birds).
caught(tom,bird1).
ako(cats,mammals).
ako(mammals,animals).
ako(birds,animals).
like(cats,cream).
owns(john,tom).
sat_on(cat1,mat1).
is_coloured(tom,ginger).
have(mammals,fur).

You might also like