0% found this document useful (0 votes)
10 views10 pages

Answer

The document contains lab assignment questions and answers for a course on Artificial Intelligence. It includes Prolog programs for listing addresses, checking passwords, calculating quadratic equation roots, predicting living organisms based on characteristics, and solving the Towers of Hanoi problem. Each program is structured with predicates and main functions to execute the tasks described.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Answer

The document contains lab assignment questions and answers for a course on Artificial Intelligence. It includes Prolog programs for listing addresses, checking passwords, calculating quadratic equation roots, predicting living organisms based on characteristics, and solving the Towers of Hanoi problem. Each program is structured with predicates and main functions to execute the tasks described.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name : Deepak Singh

Admission number : I20MA060

Subject : Artificial Intelligence

Lab Assignment 4 Questions:

1. Write a program that list four addresses in a label form, each address
should list a name, one-line address, city, state & ZIP code. (Use for in

PROLOG using appropriate syntax).

Answer :
% Define a person's information as a predicate
person(Name, Address, City, State, Zip) :-
write('Enter name: '), read(Name),

write('Enter address: '),

read(Address),

write('Enter city: '),


read(City), write('Enter

state: '), read(State),


write('Enter zip code: '),

read(Zip).

% Use a for loop to gather information for 4 people main :-


forall(between(1, 4, _), (person(Name, Address, City, State, Zip),

write(Name), write(', '), write(Address), write(', '),

write(City), write(', '),


write(State), write(', '),

write(Zip), nl)).
2. Write a program checking for Password.
A) Give an opportunity to user to re-enter the password „n‟ no. of Times,
on entering wrong password.
B) Give an opportunity to user to re-enter the password three (03)
Times, on entering wrong password.
Answer :
(A)
% Define the correct password password(secret).

% Predicate to check if entered password is correct


check_password(Attempt) :- password(Correct),
(Attempt == Correct -> write('Access granted.');

write('Access denied. Please try again.'),


read(NewAttempt),
check_password(NewAttempt)

).

% Main predicate to run the program


run_password_check :-

write('Enter password: '),

read(Attempt),
check_password(Attempt).
(B)
% Define the correct password password(secret).

% Predicate to check if entered password is correct


check_password(Attempt, TimesLeft) :-
password(Correct), (Attempt == Correct ->

write('Access granted.'); (TimesLeft > 0 ->


write('Access denied. Please try again.'),
NewTimesLeft is TimesLeft - 1,
read(NewAttempt),

check_password(NewAttempt, NewTimesLeft);

write('Access permanently denied.')


)
).
% Main predicate to run the program
run_password_check :-

write('Enter password: '),


read(Attempt),

check_password(Attempt, 3).

3. Write a program to calculate the roots of quadratic equation Consider all


possibilities real, equal, imaginary.

Answer :
% Main predicate to calculate the roots of a quadratic equation quadratic_roots(A,

B, C, X1, X2) :-

Discriminant is B^2 - 4*A*C,

(Discriminant > 0 ->


X1 is (-B + sqrt(Discriminant)) / (2*A),
X2 is (-B - sqrt(Discriminant)) / (2*A),
write('The roots are real and different.'), nl,
write('First root: '), write(X1), nl, write('Second

root: '), write(X2), nl

; Discriminant =:= 0 ->


X1 is -B / (2*A),
X2 is X1,

write('The roots are real and equal.'), nl,


write('First root: '), write(X1), nl, write('Second
root: '), write(X2), nl

; Discriminant < 0 ->


RealPart is -B / (2*A),

ImaginaryPart is sqrt(-Discriminant) / (2*A),


X1 =complex(RealPart , ImaginaryPart), X2
=complex(RealPart , -ImaginaryPart), write('The

roots are complex and different.'), nl,


write('First root: '), write(X1), nl, write('Second
root: '), write(X2), nl

).

% Main predicate to take input from user and call the above predicate
main :- write('Enter the values of a, b, and c separated by spaces: '),
read(A), read(B), read(C), quadratic_roots(A, B, C, X1, X2).
4. Predict the Living organism from the given characteristics dataset.
Represent the characteristics of Living organism in form of predicates in
Prolog. You are supposed to make a question answering system, where the
questions to user be Example: Who many legs does your Living organism
have?
Characteristics of Animal: Generally Animals takes Nutrition by eating
herbs/bushes. They are having four legs. Animals generally have one

tail. They stay in cave. They have two eyes. They can breathe.
Characteristics of Birds: Birds eats worms and grains. They have one

beak. They have feathers. They can fly. They stay in nest. They have two
eyes. They can breathe

Characteristics of Plants: Plants take nutrition from sunlight and


water. They are generally green in color. They don’t have legs, eyes.
They can breathe.
Answer :
animal(A) :- eats(A, herbs_bushes), legs(A, 4), tail(A, 1), stay_in(A, cave), eyes(A, 2),
can_breathe(A).
bird(B) :- eats(B, worms_grains), beak(B, 1), feathers(B), can_fly(B), stay_in(B, nest), eyes(B, 2),
can_breathe(B).

plant(P) :- takes_nutrition_from(P, sunlight_water), color(P, green), no_legs(P), no_eyes(P),


can_breathe(P).
eats(A, herbs_bushes) :- A = animal.

eats(B, worms_grains) :- B = bird.


legs(A, 4) :- A = animal. tail(A, 1) :- A

= animal. stay_in(A, cave) :- A =


animal. stay_in(B, nest) :- B = bird.

eyes(A, 2) :- A = animal. eyes(B, 2) :-


B = bird. can_breathe(A) :- A =
animal. can_breathe(B) :- B = bird.

can_breathe(P) :- P = plant beak(B,


1) :- B = bird. feathers(B) :- B = bird.

can_fly(B) :- B = bird.
takes_nutrition_from(P,
sunlight_water) :- P = plant.
color(P, green) :- P = plant.

no_legs(P) :- P = plant. no_eyes(P)


:- P = plant.

% Define a predicate for answering questions about the characteristics of living organisms
characteristics(A, legs, X) :- legs(A, X).

characteristics(A, tail, X) :- tail(A, X).


characteristics(A, eats, X) :- eats(A, X).

characteristics(A, stay_in, X) :- stay_in(A, X). characteristics(A, eyes, X) :-


eyes(A, X). characteristics(A, beak, X) :- beak(A, X). characteristics(A,
color, X) :- color(A, X). characteristics(A, takes_nutrition_from, X) :-
takes_nutrition_from(A, X).

5. Solve Towers of Hanoi Problem (Concept of recursion in Prolog) Towers of


Hanoi is puzzle to move N disks from the source peg/tower to the target
peg/tower using the intermediate peg as an auxiliary holding peg. There are

two conditions that are to be followed while solving this problem


a. A larger disk cannot be placed on a smaller disk.
b. Only one disk can be moved at a time.

Answer :
move(1, X, Y, _) :- write('Move disk

1 from '), write(X),

write(' to '), write(Y), nl.

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).

You might also like