Record programs in Prolog
1. WAP to find the maximum of two numbers
Aim: Finding the maximum of 2 numbers
Algorithm:
1. Start
2. If A ≥ B → Max = A
3. Otherwise → Max = B
4. Stop
Program:
KBase:
max(A, B, A) :- A >= B.
max(A, B, B) :- B > A.
Query: ?- max(2, 5, M).
Output: M = 6
2. WAP to check the given number is even/odd.
Aim: Checking the given number is even/odd
Algorithm:
1. Start
2. Divide number(N) by 2.
3. If remainder(R) = 0 then
3.1 print “Even”
4. Else
4.1 print “Odd”
5. Stop
Program:
KBase:
check_even_odd(N) :-
R is N mod 2,
R =:= 0,
write('Even'), !
; write('Odd').
Output: ?- check_even_odd(5).
Output: Odd
3. WAP to find the factorial value of a given number.
Aim: Finding the factorial value of a given number
Algorithm:
1. Start
2. If N = 0, factorial is 1.
1
3. Otherwise:
3.1 Compute factorial of (N − 1).
3.2 Multiply N with result.
4. Return the factorial value.
5. Stop
Program:
KBase:
% Base case
factorial(0, 1) :- !.
% Recursive case
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
Query: ?- factorial(5, F).
Output: F = 120
4. WAP to generate Fibonacci series
Aim: To print all Fibonacci values upto the given value(<=N).
Algorithm:
1. Start
2. Input NNN
3. Initialize:
3.1 First = 0
3.2 Second = 1
4. While First ≤ N:
4.1 Print First
4.2 Next = First + Second
4.3 First = Second
4.4 Second = Next
5. Stop
Program:
KBase:
% Main predicate
fibonacci_upto(N) :- N >= 0, fib_range(0, 1, N).
% Stop when current number exceeds N
fib_range(A, _, N) :- A > N, !.
% Print and continue
fib_range(A, B, N) :-
write(A), nl,
Next is A + B,
fib_range(B, Next, N).
Query: ?- Fibonacci_upto(10).
Output:
0
1
1
2
2
3
5
8
True
5. WAP to find sum of all list elements
Aim: To find sum of all list elements
Algorithm:
1. Start
2. If list is empty, return 0
3. Otherwise:
3.1 Take first element
3.2 Find sum of remaining list
3.3 Add first element to sum
4. Return result
5. Stop
Program:
KBase:
sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, S1),
Sum is H + S1.
Query: ?- sum_list([1,2,3,4], S).
Output: S=10
6. WAP to implement monkey-banana problem.
Aim: To write a program a monkey gets the banana
Initial State:
Monkey at position A
Box at position B
Banana at position C
Monkey is on floor
Algorithm:
1. Start
2. If monkey already has banana → Stop
3. If monkey is not at box:
3.1 Move monkey to box position
4. If box is not under banana:
4.1 Push box to banana position
5. If monkey is not on box:
5.1 Climb the box
6. Grab the banana
7. Stop
Program:
KBase:
3
% Define the possible actions and resulting state changes:
% move(State1, Action, State2)
% Grasp action: Monkey grasps banana when on the box in the middle
move(state(middle, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
% Climb action: Monkey climbs onto the box at location P
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).
% Push action: Monkey pushes the box from P1 to P2 while on the floor
move(state(P1, onfloor, P1, H),
push(P1, P2),
state(P2, onfloor, P2, H)).
% Walk action: Monkey walks from P1 to P2 on the floor (box position B is irrelevant)
move(state(P1, onfloor, B, H),
walk(P1, P2),
state(P2, onfloor, B, H)).
% Define the goal condition (base case for recursion):
% The monkey has the banana in any state
canget(state(_, _, _, has)).
% Define the recursive rule:
% The monkey can get the banana in State1 if there is a move to State2
% and the monkey can get the banana from State2
canget(State1) :- move(State1, _Action, State2), canget(State2).
Query: canget(state(atdoor, onfloor, atwindow, hasnot)).
Output: true
7. WAP to find a path from Source to Destination.
Aim: To find a path from source to destination
Algorithm:
1. Start
2. Input Source and Destination
3. Check if there is a direct connection:
3.1 If yes, return the route
4. Otherwise:
4.1Find a city connected to Source
4.2 Move to that city
4.3Mark Source as visited
5. Repeat the process for the new city
6. Avoid revisiting already visited cities (to prevent loops)
7. Continue until Destination is reached
4
8. Return the complete path
9. Stop
Program:
KBase:
%Facts, route(Source, Destination, Cost)
route(delhi, mumbai, 5).
route(delhi, kolkata, 4).
route(mumbai, chennai, 6).
route(kolkata, chennai, 3).
route(chennai, bangalore, 2).
% Rule 1, direct travel
travel(X, Y, [X,Y], Cost) :- route(X, Y, Cost).
% Rule 2, indirect travel
travel(X, Y, [X|Path], Cost) :- route(X, Z, Cost1), travel(Z, Y, Path, Cost2), Cost is Cost1 +
Cost2.
Query(To find path from Delhi to Bangalore):
?- travel(delhi, bangalore, Path, Cost).
Output:
Cost = 13,
Path = [delhi, mumbai, chennai, bangalore]
Cost=9,
Path = [delhi, kolkata, chennai, bangalore]
8. WAP to implement Eight Queens problem.
Aim: To implement 8 queens problem
The Eight Queens Problem is to place 8 queens on an 8×8 chessboard so that no two queens
attack each other. Such that:
No 2 queens are placed on same row
No 2 queens are placed on same column
No 2 queens are placed on same diagonal
In Prolog, this problem is solved using backtracking and recursion.
Algorithm:
1. Start
2. Represent the board using a list of 8 positions
2.1 Each position represents a column
2.2 Value represents the row number of queen
3 Place queens column by column
4 For each column:
4.1 Try placing queen in each row (1 to 8)
4.2 Check:
4.2.1 No same row conflict
4.2.2 No diagonal conflict
5 If safe:
5.1 Place queen and move to next column
5
6 If not safe:
6.1 Try next row
7 If no row works:
7.1 Backtrack to previous column
8 Repeat until all 8 queens are placed
9 Output the solution
10 Stop
Program:
KBase:
% main predicate
queens(Solution) :- permutation([1,2,3,4,5,6,7,8], Solution), safe(Solution).
% check all queens are safe
safe([]).
safe([Q|Qs]) :- safe(Qs), no_attack(Q, Qs, 1).
% check no diagonal attack
no_attack(_, [], _).
no_attack(Q, [Q1|Qs], Dist) :- abs(Q - Q1) =\= Dist, Dist1 is Dist + 1, no_attack(Q,
Qs, Dist1).
Query: ?- queens(S).
Output: S = [1, 5, 8, 6, 3, 7, 2, 4]
S = [1, 6, 8, 3, 7, 4, 2, 5]
S = [1, 7, 4, 6, 8, 2, 5, 3]
S = [1, 7, 5, 8, 2, 4, 6, 3] ........etc.