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

Prolog Assignment

The document contains a series of Prolog programming assignments that cover various fundamental concepts such as calculating sums, finding maximum values, computing factorials, generating Fibonacci numbers, and implementing GCD. It also includes programs for exponentiation, multiplication, the Tower of Hanoi, membership checking in lists, list concatenation, list reversal, and palindrome checking. Each assignment provides a Prolog code snippet demonstrating the implementation of the respective function.

Uploaded by

singhavishu372
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 views3 pages

Prolog Assignment

The document contains a series of Prolog programming assignments that cover various fundamental concepts such as calculating sums, finding maximum values, computing factorials, generating Fibonacci numbers, and implementing GCD. It also includes programs for exponentiation, multiplication, the Tower of Hanoi, membership checking in lists, list concatenation, list reversal, and palindrome checking. Each assignment provides a Prolog code snippet demonstrating the implementation of the respective function.

Uploaded by

singhavishu372
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/ 3

Assignment

1. Write a prolog program to calculate the sum of two numbers.

sum(X,Y,Z):- Z is X+Y.

2. Write a Prolog program to implement max(X, Y, M) so that M is the maximum of


two numbers X and Y

max(X,Y,M):-X>Y,M is X. max(X,Y,M):-Y>=X,M is Y.

3. Write a program in PROLOG to implement factorial (N, F) where F represents the


factorial of a number N.

fact(0,1). fact(N,X):-N1 is N-1,fact(N1,Y),X is Y*N,!.

4. Write a program in PROLOG to implement generate_fib(N,T) where T represents


the Nth term of the fibonacci series.

fib(1,0).

fib(2,1).

fib(N,X):- N1 is N-1,N2 is N-2,

fib(N1,X1),fib(N2,X2),X is X1+X2,!.

5. Write a Prolog program to implement GCD of two numbers.

gcd(0,A,A):-!.

gcd(A,0,A):-!.

gcd(A,B,R):-B1 is mod(A,B),gcd(B,B1,R).

Dr. Anirban Chakraborty Page 1


6. Write a Prolog program to implement power (Num,Pow, Ans) : where Num is
raised to the power Pow to get Ans.

power(X,0):- !.

power(Num,Pow,Ans):-Ans is Num^Pow.

7. Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the
numbers to be multiplied and R represents the result.

multi(X,0).

multi(N1,N2,R):- R is N1*N2.

8. Write a program in PROLOG to implement tower of hanoi (N) where N represents


the number of discs.

move(1,X,Y,_):- write('Move disk 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).

tower_of_hanoi(N) :- move(N,left,right,center).

Dr. Anirban Chakraborty Page 2


9. Write a Prolog program to implement memb(X, L): to check whether X is a
member of L or not.

memb(X,[X|Tail]).

memb(X,[Head|Tail]):-memb(X,Tail).

Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with
L1 to get the resulted list L3.

conc([],L,L).

conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).

10. Write a Prolog program to implement reverse (L, R) where List L is original and
List R is reversed list.

append([],L,L).

append([X|L1],L2,[X|L3]):- append(L1,L2,L3).

reverse([],[]).

reverse([H|T],R):- reverse(T,L1),append(L1,[H],R).

11. Write a program in PROLOG to implement palindrome (L) which checks whether a
list L is a palindrome or not.

app([],L,L).

app([X|L1],L2,[X|L3]):- app(L1,L2,L3).

pal([]). pal([_]).

pal(Plist):-app([H|T],[H],Plist),pal(T).

________

Dr. Anirban Chakraborty Page 3

You might also like