0% found this document useful (0 votes)
7 views12 pages

Prolog Assignment

The document contains a collection of Prolog programs that demonstrate various programming concepts, including arithmetic operations, list manipulation, and recursion. Each program is accompanied by a brief description and its corresponding code implementation. Topics covered include calculating sums, finding maximum values, factorials, Fibonacci numbers, and more.

Uploaded by

sannigrahi.jeet
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)
7 views12 pages

Prolog Assignment

The document contains a collection of Prolog programs that demonstrate various programming concepts, including arithmetic operations, list manipulation, and recursion. Each program is accompanied by a brief description and its corresponding code implementation. Topics covered include calculating sums, finding maximum values, factorials, Fibonacci numbers, and more.

Uploaded by

sannigrahi.jeet
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/ 12

1

INDEX
Sl.no Program Page No. Signature

1. Write a prolog program to calculate the sum 2


of two numbers.

2. Write a prolog program to find the maximum 3


of two numbers.

3. Write a prolog program to calculate the 4


factorial of a given number.

4. Write a prolog program to calculate the nth 5


Fibonacci number.

5. Write a Prolog program to implement append 6


for two lists
6. Write a Prolog program to implement 7
palindrome(List).
7. Write a Prolog program to implement 8
max(X,Y,Max) so that Max is the greater of
two numbers X and Y.
8. Write a Prolog program to implement 9
maxlist(List,Max) so that Max is the greatest
number in the list of numbers List.
9. Write a Prolog program to implement 10
sumlist(List,Sum) so that Sum is the sum of a
given list of numbers List.
10. Write a Prolog program to implement 11
reverse(List,ReversedList) that reverses lists.

11. Write a Prolog program to implement GCD of 12


two numbers.
2

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

Program:-

go:-write('Enter the first number:'),read(X),nl,

write('Enter the second number:'),read(Y),nl,add(X,Y).

add(X,Y):-S is X+Y,
write('Sum='),write(S).

Output:-
3

2. Write a prolog program to find the maximum of two numbers.

Program:-

go:-write('Enter the first number:'),read(X),nl,

write('Enter the second number:'),read(Y),nl,max(X,Y).

max(X,Y):-
(

X=Y ->
write('both are equal');

X>Y ->

Z is X,
write('Big number='),write(Z)

);

(
Z is Y,

write('Big number='),write(Z)

)
).

Output:-
4

3. Write a prolog program to calculate the factorial of a given number.

Program:-

fact(0,1).

fact(N,F):-

(
% The below is for +ve factorial.

N>0 ->
(

N1 is N-1,

fact(N1,F1),

F is N*F1
)

% The below is for -ve factorial.


N<0 ->

N1 is N+1,
fact(N1,F1),

F is N*F1

).

Output:-
5

4. Write a prolog program to calculate the nth Fibonacci number.

Program:-

fib(0,1):-!.

fib(1,1):-!.

fib(N,F):-
N>1,

N1 is N-1,
N2 is N-2,

fib(N1,F1),

fib(N2,F2),

F is F1+F2.

Output:-
6

5. Write a Prolog program to implement append for two lists.

Program:-

conc([],L,L).

conc([X|M],N,[X|Q]):-

conc(M,N,Q).

Output:-
7

6. Write a Prolog program to implement palindrome(List).

Program:-

palind([]):- write('palindrome').

palind([_]):- write('palindrome').

palind(L) :-
append([H|T], [H], L),

palind(T)
;

write('Not a palindrome').

Output:-
8

7. Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X
and Y.

Program:-

max(X,Y,R):-
X>=Y ->

R is X,

write(R)

;
R is Y,

write(R).

grandiose([H|T],R):-
H>T ->

R is H,

write(R)

;
R is T,

write(T).

Output:-
9

8. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the
list of

numbers List.

Program:-

maxlist([H|T],R):-

length(T,L),

L>0 ->

(
maxlist(T,R1),

(
H > R1 ->

R is H

;
R is R1

;
R is H.

Output:-
10

9. Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of

numbers List.

Program:-

sumlist([],0).
sumlist([H|T],R):-

sumlist(T,R1),

R is H+R1.

Output:-
11

10. Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

Program:-

conc([],L,L).

conc([H|T],L2,[H|L3]):-conc(T,L2,L3).

revlist([],[]).

revlist([H|T],R):-revlist(T,Trev),conc(Trev,[H],R).

Output:-
12

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

Program:-

gcd(X,0,X).

gcd(X,Y,Z):-

R is mod(X,Y),
gcd(Y,R,Z).

Output:-

You might also like