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

AI Remaining

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

INDEX

Sr.No Topic Sign


Write a prolog program to calculate the sum of two numbers
1
Write a Prolog program to implement max(X, Y, M) so
2 that M is the maximum of two numbers X and Y.
Write a program in PROLOG to implement factorial (N,
3 F) where F represents the factorial of a number N.
Write a Prolog program to implement member(A, S): to check
4 whether A is a member of S or not
Write a Prolog program to implement multiply(X1, X2, M) :
5 where X1 and X2 denotes the numbers to be multiplied and
M represents the result
Write a Prolog program to implement GCD of two numbers
6
Write a program in PROLOG to implement palindrome (S)
7 which checks whether a list S is a palindrome or not
Write a Prolog program to implement maxlist(S, M); minlist(S,
8 M) so that M is the maximum/minimum number in the list
Write a Prolog program to merge two lists
9
Write a prolog program to implement insert_nth (I, N, S, R)
10 that inserts an item I into Nth position of list S to generate a
list R
Write a prolog program to implement delete_nth (N, S, R)
11 that removes Nth position item of list S to generate a list R
Write a Prolog program for BFS (Breadth First Search)
12
Write a Prolog program for DFS (Depth First Search)
13
Write a Prolog program for Family Tree
14
Write a Prolog program for Calculator
15
Write a Prolog program for various List operations
16
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:
12. 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:
13. 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:
14. 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:
15. 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:
16. 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:

You might also like