1. Write a prolog program to calculate the sum of two numbers.
Code:
% Predicate to calculate the sum
sum(X,Y):-
S is X+Y,
write(S).
Output:
2. Write a Prolog program to implement max(X, Y, M) so that M is the
maximum of two numbers X and Y.
Code:
% Predicate to calculate the maximum
max(X, Y, M) :-
( X>Y
-> M = X
; M=Y
).
Output:
3. Write a program in PROLOG to implement factorial (N, F) where F
represents the factorial of a number N.
Code:
% Base case: factorial of 0 is 1
factorial(0, 1).
% Handling negative numbers
factorial(N, error(factorial_is_not_defined_for_negative_numbers))
:-
integer(N),
N < 0.
% Recursive case: N! = N * (N-1)!
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
Output:
4. Write a Prolog program to implement member(A, S): to check whether
A is a member of S or not.
Code:
% Base case: A is a member of the list [A|_]
member(A, [A|_]).
/* Recursive case: A is a member of the list [_|Tail] if A is a member of
Tail */
member(A, [_|Tail]) :- member(A, Tail).
Output:
5. Write a Prolog program to implement multiply(X1, X2, M) : where X1
and X2 denotes the numbers to be multiplied and M represents the
result.
Code:
% Predicate to calculate the multiply
multiply(X1, X2, M) :-
M is X1 * X2.
Output:
6. Write a Prolog program to implement GCD of two numbers.
Code:
% Base case: GCD of X and 0 is X
gcd(X, 0, X).
% Recursive case: GCD of X and Y is the GCD of Y and X mod Y
gcd(X, Y, GCD) :-
Y > 0,
R is X mod Y,
gcd(Y, R, GCD).
Output:
7. Write a program in PROLOG to implement palindrome (S) which checks
whether a list S is a palindrome or not.
Code:
% Base case: an empty list and a single element list are palindromes
palindrome([]).
palindrome([_]).
/* Recursive case: a list is a palindrome if the first and last elements are
the same */
% and the sublist without the first and last elements is a palindrome
palindrome([Head|Tail]) :-
append(Mid, [Head], Tail),
palindrome(Mid).
Output:
8. Write a Prolog program to implement maxlist(S, M); minlist(S, M) so
that M is the maximum/minimum number in the list.
Code:
% maxList(S, M) :- M is max_list(S).
% minList(S, M) :- M is min_list(S).
% Alternative implementation for maxList/2
maxList([X], X).
maxList([H|T], M) :-
maxList(T, M1),
M is max(H, M1).
% Alternative implementation for minList/2
minList([X], X).
minList([H|T], M) :-
minList(T, M1),
M is min(H, M1).
Output:
9. Write a Prolog program to merge two lists.
Code:
% Base case: Append empty list to another list
merge([], L, L).
% Recursive case: Merge heads and tails
merge([H|T1], L2, [H|T]) :-
merge(T1, L2, T).
% Alternative implementation using append/3
merge(L1, L2, L) :-
append(L1, L2, L).
Output:
10. Write a prolog program to implement insert_nth (I, N, S, R) that inserts
an item I into Nth position of list S to generate a list R
Code:
% Base case: Insert at 1st position
insert_nth(I, 1, S, [I|S]).
% Recursive case: Insert at Nth position
insert_nth(I, N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
insert_nth(I, N1, T, R).
Output:
11. Write a prolog program to implement delete_nth (N, S, R) that
removes Nth position item of list S to generate a list R.
Code:
% Base case: Delete 1st position
delete_nth(1, [_|T], T).
% Recursive case: Delete Nth position
delete_nth(N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
delete_nth(N1, T, R).
Output:
13. Write a Prolog program for BFS (Breadth First Search) .
Code:
% Define the graph as a list of edges
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).
% BFS predicate
bfs(Start, Goal, Path) :- bfs([Start], [Start], Goal, Path).
% Helper predicate
bfs([], _, _, _) :- fail.
bfs([Node|Nodes], Visited, Goal, Path) :-
( Node = Goal
-> reverse(Visited, Path)
; findall(NewNode, (edge(Node, NewNode), \+ member(NewNode,
Visited)), NewNodes),
append(Nodes, NewNodes, NextNodes),
bfs(NextNodes, [Node|Visited], Goal, Path)
).
Output:
14.Write a Prolog program for DFS (Depth First Search) .
Code:
% Define the graph as a list of edges
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).
% DFS predicate
dfs(Start, Goal, Path) :- dfs([Start], Start, Goal, Path).
% Helper predicate
dfs(Visited, Node, Node, [Node|Visited]).
dfs(Visited, Start, Goal, Path) :-
edge(Start, Next),
\+ member(Next, Visited),
dfs([Next|Visited], Next, Goal, Path).
Output:
15.Write a Prolog program for Family Tree.
Code:
%% Define family relationships
father(john, emily).
father(john, michael).
mother(mary, emily).
mother(mary, michael).
father(david, sarah).
mother(emily, sarah).
father(michael, oliver).
mother(sarah, oliver).
%% Define gender
male(john).
male(michael).
male(david).
male(oliver).
female(mary).
female(emily).
female(sarah).
%% Define parent and child relationships
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
child(X, Y) :- parent(Y, X).
%% Define sibling relationship
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X \= Y.
%% Define sister relationship
sister(X, Y) :-
sibling(X, Y),
female(X).
%% Define brother relationship
brother(X, Y) :-
sibling(X, Y),
male(X).
%% Define grandparent relationship
grandparent(X, Y) :-
parent(X, Z),
parent(Z, Y).
%% Define grandfather relationship
grandfather(X, Y) :-
grandparent(X, Y),
male(X).
%% Define grandmother relationship
grandmother(X, Y) :-
grandparent(X, Y),
female(X).
Output:
16.Write a Prolog program for Calculator .
Code:
%% Arithmetic Operations
%% sum(A, B) calculates the sum of A and B
sum(A, B) :-
C is A + B,
write('Result: '), write(C), nl.
%% diff(A, B) calculates the difference of A and B
diff(A, B) :-
C is A - B,
write('Result: '), write(C), nl.
%% multi(A, B) calculates the product of A and B
multi(A, B) :-
C is A * B,
write('Result: '), write(C), nl.
%% divide(A, B) calculates the quotient of A and B
divide(A, B) :-
B =\= 0,
C is A / B,
write('Result: '), write(C), nl.
divide(A, 0) :-
write('Error: Division by zero!'), nl.
%% sqr(N, M) calculates N raised to the power of M
sqr(N, M) :-
Z is N ** M,
write('Result: '), write(Z), nl.
%% Trigonometric Operations
%% trigo(P, B, H) calculates trigonometric functions given perpendicular
(P), base (B), and hypotenuse (H)
trigo(P, B, H) :-
Sinx is P / H,
write('sin(x) = '), write(Sinx), nl,
Cosx is B / H,
write('cos(x) = '), write(Cosx), nl,
Tanx is P / B,
write('tan(x) = '), write(Tanx), nl,
Cosecx is H / P,
write('cosec(x) = '), write(Cosecx), nl,
Secx is H / B,
write('sec(x) = '), write(Secx), nl,
Cotx is B / P,
write('cot(x) = '), write(Cotx), nl.
Output:
17.Write a Prolog program for various List operations .
Code:
%% List Operations
%% Append
append([], L, L).
append([H|T], L, [H|R]) :- append(T, L, R).
%% Length
length([], 0).
length([_|T], N) :- length(T, N1), N is N1 + 1.
%% Reverse
reverse(L, R) :- reverse(L, [], R).
reverse([], R, R).
reverse([H|T], Acc, R) :- reverse(T, [H|Acc], R).
%% Member
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
%% Delete
delete([], _, []).
delete([X|T], X, T).
delete([H|T], X, [H|R]) :- delete(T, X, R).
%% Insert
insert(X, [], [X]).
insert(X, [H|T], [X, H|T]).
insert(X, [H|T], [H|R]) :- insert(X, T, R).
%% Sort
sort([], []).
sort([X|T], S) :-
sort(T, S1),
insert(X, S1, S).
%% Maximum
max([X], X).
max([X|T], M) :-
max(T, M1),
M is max(X, M1).
%% Minimum
min([X], X).
min([X|T], M) :-
min(T, M1),
M is min(X, M1).
%% Sum
sum([], 0).
sum([H|T], S) :-
sum(T, S1),
S is H + S1.
Output: