Q1. Write a program to find the factorial of any number. Test it with 11.
Code:
Predicates
factorial(integer,integer)
Clauses
factorial(0,1).
factorial(N,F):-N>0,N1=N-1,factorial(N1,F1),F=N*F1.
Goal
factorial(11,F).
Jan 29, 2010
Fibonacci series
Q2. Write a program to find the Fibonacci series of the number being entered. Test it for number 10.
PREDICATES
fibo(integer,integer)
CLAUSES
fibo(0,0).
fibo(1,1).
fibo(N,F):-N>0,N1=N1,N2=N2,fibo(N1,F1),fibo(N2,F2),F=F1+F2.
GOAL
fibo(10,F).
PREDICATES
fibo(integer,integer)
CLAUSES
fibo(0,0).
fibo(1,1).
fibo(N,F):-N>0,N1=N1,N2=N2,fibo(N1,F1),fibo(N2,F2),F=F1+F2.
GOAL
fibo(10,F).
Define a predicate to output the values of the squares of the integer from N1 to N2 inclusive and test it with N1=6 and N2=12
Q3. Define a predicate to output the values of the squares of the integer from N1 to N2 inclusive and test it with N1=6 and N2=12
Code :
predicates
testloop(integer)
clauses
testloop(5).
testloop(N):-N>=6,write("square of "),write(N),write(" is "),N1=N*N,write(N1),nl,M=N-1,testloop(M).
Goal
testloop(12).
Code :
predicates
testloop(integer)
clauses
testloop(5).
testloop(N):-N>=6,write("square of "),write(N),write(" is "),N1=N*N,write(N1),nl,M=N-1,testloop(M).
Goal
testloop(12).
Subscribe to:
Posts (Atom)