INDEX
SOURCE CODE PAGE NO.
1. Write a prolog program to calculate the sum of two numbers 2
2. Write a prolog program to find the maximum of two numbers 3
3. Write a prolog program to calculate the gcd of two numbers 4
4. Write a prolog program o calculate the factorial of a given number 5
5. Write a prolog program to calculate the nth fibonacci number 6
6. Write a prolog program to implement palindrome (list) 7
7. Write a prolog program to implement max list (List, Max) so that Max is
Greatest number in the list of numbers list 8
8. Write a prolog program to remove the Nth item from a list 9
9. Write a prolog program, remove-nth (Before, After) that asserts the after list is
The Before list with the removal of every n’th item from every list at all levels 10
10. Write a prolog program to implement append for two lists 11
11. Write a prolog program to implement sum list (List, Sum) so that sum
Is the sum of a given list of numbers list 12
12. Write a prolog program to implement reverse ( List, Reversed List ) that reverses lists 13
1
Date:06/03/2023
Q1. Write a prolog program to calculate the sum of two numbers.
Source code: -
sum(X,Y):-
S is X+Y,
write(S).
Output:-
TEACHER’S SIGNATURE
2
Date:13/03/2023
Q2. Write a prolog program to find the maximum of two
numbers.
Source code:-
max(X,Y):-
(
X=Y ->
write('both are equal') ;
X>Y ->
(
Z is X,
write(Z)
);
(
Z is Y,
write(Z)
)
).
Output:-
TEACHER’S SIGNATURE
3
Date:20/03/2023
Q3. Write a prolog program to calculate the gcd of two numbers.
Source code:-
gcd(X,0,X).
gcd(X,Y,Z):-
R is mod(X,Y),
gcd(Y,R,Z).
Output: -
TEACHER’S SIGNATURE
4
Date:13/04/2023
Q4. Write a prolog program to calculate the factorial of a given
number.
Source code: -
fact(0,1).
fact(N,F):-
(
N>0 ->
(
N1 is N-1,
fact(N1,F1),
F is N*F1
);
N<0 ->
(
N1 is N+1,
fact(N1,F1),
F is N*F1
)
).
Output:-
5
Date:24/04/2023
Q5. Write a prolog program to calculate nth Fibonacci number.
Source code: -
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.
Output:-
TEACHER’S SIGNATURE
6
Date:06/03/2023
Q6. Write a prolog program to implement Palindrome (List).
Source code:-
palind([]):- write('palindrome').
palind([_]):- write('palindrome').
palind(L) :-
append([H|T], [H], L),
palind(T) ;
write('Not a palindrome').
Output:-
TEACHER’S SIGNATURE
7
Date:06/03/2023
Q7. Write a Prolog program to implement maxlist(List,Max) so
that Max is the greatest number in the list of numbers List.
Source code:-
max2([H],H).
max2([H|T],R):-
max2(T,M1),
H>=M1,
R is H,!.
max2([H|T],R):-
max2(T,M1),
H<M1,
R is M1.
Output:-
TEACHER’S SIGNATURE
8
Date:06/03/2023
Q8. Write a Prolog program to remove the Nth item from a list.
Source code:-
del(X,[X|Tail],Tail).
del(X,[Head|Tail],[Head|NewTail]):-
del(X,Tail,NewTail).
Output:-
TEACHER’S SIGNATURE
9
Date:06/03/2023
Q9. Write a Prolog program, remove-nth(Before, After) that
asserts the After list is the Before list with the removal of every
n‘th item from every list at all levels.
Source code:-
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).
daltob(P,L,R):-
length(L,L1),
P=:=1 ->
P3 is P+1,
delte(P3,L,R);
P=:=L1 ->
P3 is P-1,
delte(P3,L,R);
P1 is P-1,
delte(P1,L,R1),
delte(P,R1,R)
).
Output:-
TEACHER’S SIGNATURE
10
Date:06/03/2023
Q10. Write a Prolog program to implement append for two lists.
Source code:-
conc([],L,L).
conc([X|M],N,[X|Q]):-
conc(M,N,Q).
Output:-
TEACHER’S SIGNATURE
11
Date:06/03/2023
Q11. Write a Prolog program to implement sumlist(List,Sum) so
that Sum is the sum of a given list of numbers List.
Source code:-
sumlist([],0).
sumlist([H|T],R):-
sumlist(T,R1),
R is H+R1.
Output:-
TEACHER’S SIGNATURE
12
Date:06/03/2023
Q12. Write a Prolog program to implement
reverse(List,ReversedList) that reverses lists.
Source code:-
reverse([H|T],R):-
length(T,L),
L>0 ->
(
reverse(T,R1),
R is H
);
R is H.
Output:-
TEACHER’S SIGNATURE
13