0% found this document useful (0 votes)
48 views

To Take Two Input As Integer and Print The Greater One.: Prolog Lab

This document contains 4 Prolog labs involving input/output, domains, comparisons, equality, and random numbers. Lab 3_1 takes user input and prints corresponding responses. Lab 3_2 takes two integer inputs and prints the greater number. Lab 3_3 uses equality (=) to test if two values are equal or not. Lab 4_4 uses random/1 to randomly select and print 3 names out of a list of 5 names.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
48 views

To Take Two Input As Integer and Print The Greater One.: Prolog Lab

This document contains 4 Prolog labs involving input/output, domains, comparisons, equality, and random numbers. Lab 3_1 takes user input and prints corresponding responses. Lab 3_2 takes two integer inputs and prints the greater number. Lab 3_3 uses equality (=) to test if two values are equal or not. Lab 4_4 uses random/1 to randomly select and print 3 names out of a list of 5 names.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 2

[] PROLOG

LAB

LAB #3

Input/Output.
Use the Domains.
Comparisons, Equality.
Random number.

%Lab3_1:
PREDICATES
action(integer)
CLAUSES
action(1):action(2):action(3):action(N):-

nl,
write("You typed 1."), nl.
nl,
write("You typed two."), nl.
nl,
write("Three was what you typed."), nl.
nl,
N<>1, N<>2, N<>3,
write("I don't know that number!").

GOAL
write("Type a number from 1 to 3: "),
readint(Num),
action(Num).

%Lab3_2: To take two input as integer and print the greater one.
domains
i = integer
predicates
greater ( i, i)
clauses
greater(X,Y):- X >Y, write(the greater is,X).
greater(X,Y):- write ( the greater is ,Y).
goal
greater(4, 3).
-------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl
1

[] PROLOG

%Lab3_3:

LAB

uses Equality(=) statement.

PREDICATES
test(real, real)
CLAUSES
test(X, X) :- !, write("ok \n").
test(X, Y) :- Diff = X-Y, write( X, " <> " , Y , "\n X-Y = ", Diff ).
GOAL
X = 47 ,
%GOAL

%Lab3_4:

Y = 4.7 * 10 , test(X,Y).

X = 47 ,

Y = 4.5 * 10 , test(X,Y).

uses random/1 to select three names from five at random.

PREDICATES
person(integer, symbol)
rand_int_1_5(integer)
rand_person(integer)
CLAUSES
person(1, fred).
person(2, omer).
person(3, salem).
person(4, ahmed).
person(5, aziz).
rand_int_1_5(X):- random(Y), X=Y*4+1.
rand_person(0):- !.
rand_person(Count):- rand_int_1_5(N), person(N, Name),
write(Name), nl,
NewCount=Count-1,
rand_person(NewCount).
GOAL
rand_person(3).

-------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl


2

You might also like