0% found this document useful (0 votes)
8 views19 pages

Aifinal

The document is a practical file for a course in Artificial Intelligence, authored by Piyush Kumar, detailing various Prolog programming exercises. It includes a list of tasks such as calculating sums, finding maximum values, generating Fibonacci sequences, and implementing algorithms for factorial, GCD, and more, each accompanied by code and expected outputs. The document serves as a comprehensive guide for students to practice and implement basic AI concepts using Prolog.

Uploaded by

ankitrana0srana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Aifinal

The document is a practical file for a course in Artificial Intelligence, authored by Piyush Kumar, detailing various Prolog programming exercises. It includes a list of tasks such as calculating sums, finding maximum values, generating Fibonacci sequences, and implementing algorithms for factorial, GCD, and more, each accompanied by code and expected outputs. The document serves as a comprehensive guide for students to practice and implement basic AI concepts using Prolog.

Uploaded by

ankitrana0srana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

PRACTICAL FILE for Core Paper XIII


Artificial Intelligence

Name: Piyush Kumar


Course: B. Sc. (H) Computer Science, III Year, VI Semester
College Roll No: CSC/20/30
University Roll No. 20059570022
SUBJECT: Artificial Intelligence

1|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

INDEX

Sl. Question Page


No. No.
1. Write a prolog program to calculate the sum of two numbers. 4
2. Write a Prolog program to implement max(X, Y, M) so that M 4
is the maximum of two numbers X and Y.
3. Write a program in PROLOG to implement factorial (N, F) 5
where F represents the factorial of a number N.
4. Write a program in PROLOG to implement generate_fib(N,T) 6
where T represents the Nth term of the fibonacci series.
5. Write a Prolog program to implement GCD of two numbers. 7
6. Write a Prolog program to implement power (Num,Pow, Ans) : 8
where Num is raised to the power Pow to get Ans.
7. Prolog program to implement multi (N1, N2, R) : where N1 and 9
N2 denotes the numbers to be multiplied and R represents the
result.
8. Write a Prolog program to implement memb(X, L): to check 10
whether X is a member of L or not.
9. Write a Prolog program to implement conc (L1, L2, L3) where 11
L2 is the list to be appended with L1 to get the resulted list L3.
10. Write a Prolog program to implement reverse (L, R) where List 12
L is original and List R is reversed list.
11. Write a program in PROLOG to implement palindrome (L) 12
which checks whether a list L is a palindrome or not.
12. Write a Prolog program to implement sumlist(L, S) so that S is 13
the sum of a given list L.
13. Write a Prolog program to implement two predicates 14
evenlength(List) and oddlength(List) so that they are true if their
argument is a list of even or odd length respectively.

2|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

14. Write a Prolog program to implement nth_element (N, L, X) 16


where N is the desired position, L is a list and X represents the
Nth element of L.
15. Write a Prolog program to implement maxlist(L, M) so that M 16
is the maximum number in the list.
16. Write a prolog program to implement insert_nth (I, N, L, R) that 17
inserts an item I into Nth position of list L to generate a list R.

17. Write a Prolog program to implement delete_nth (N, L, R) that 18


removes the element on Nth position from a list L to generate a
list R.
18. Write a program in PROLOG to implement merge (L1, L2, L3) 19
where L1 is first ordered list and L2 is second ordered list and
L3 represents the merged list.

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

1.1 Code
sum(A,B):-Z is A+B,write(Z).

ques1:- write('Enter 1st number :- '),nl,read(A),

write('Enter 2nd number :- '),nl,read(B),

write('Sum of '),write(A+B),write(' is

'),sum(A,B).

1.2 Output

3|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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


numbers X and Y.

2.1 Code
ques2:- write('Enter first number : '),read(X),nl,

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

max(X,Y):-

X>Y,M is X,

write('Maximum Number is: '),write(M).

max(X,Y):-

Y>X,M is Y,

write('Maximum Number is: '),write(M).

2.2 Output

4|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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


factorial of a number N.

3.1 Code

factorial(N, F) :-

( N = 0 ->

F=1
; N > 0 -> N1

is N - 1,

factorial(N1, F1),

F is N * F1

).

main :- write('Enter a number: '), read(N),

factorial(N, F), format('The factorial of ~w is

~w.~n', [N, F]).

3.2 Output

5|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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


Nth term of the fibonacci series.

4.1 Code

print_fibo(_A,_B,_C,L):- L=:=1,write('0.'). print_fibo(_A,_B,_C,L):-

L=:=0,write(' - '),nl. print_fibo(A,_B,C,_L):-

C=:=1,write(A),write('.'),nl.

print_fibo(A,B,C,L):- write(A),write(','),

E is A+B,

D is B,

F is C-1,print_fibo(D,E,F,L).

ques4:- write('Enter number of Elements you want from Febonacci Series :- '),read(A),nl,

write('Fibonacci Series :- '),print_fibo(0,1,A,A),nl.

4.2 Output

6|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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

5.1 Code

gcd(0,_X):-write("GCD is 0"). gcd(_X,0):-write("GCD

is 0"). gcd(1,_X):-write("GCD is 1"). gcd(_X,1):-

write("GCD is 1"). gcd(A,B):-startnow(A,B,2).

confirm(0,0,_A,_B,C):-write("GCD is :- "),write(C).

confirm(_X,_Y,A,B,C):- C<A,S is

C+1,startnow(A,B,S). startnow(A,B,C):-X is A mod

C,Y is B mod C,confirm(X,Y,A,B,C).

ques5:- write("Enter 1st number(Smaller) :-

"),read(A),nl, write("Enter 2nd number(Bigger) :-

"),read(B),nl, write("GCD is :- "),gcd(A,B),nl.

5.2 Output

7|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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

6.1 Code

calc_power(A,B):- Z is A^B,write(Z),nl. ques6:-

write('Enter Base Value :- '),read(Num),nl,

write('Enter Power Value :- '),read(Pow),nl,

write('Resultant Value :- '),calc_power(Num,Pow),nl.

6.2 Output

8|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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

7.1 Code

mul(A,B,_Z):- Temp is A*B,write(Temp),nl.

ques7:- write('Enter 1st number :-

'),read(A),nl, write('Enter 2nd number :-

'),read(B),nl, write('Mulitplication Result :-

'),mul(A,B,1).

9|20059570022
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

7.2 Output

8. Write a Prolog program to implement memb(X, L): to check whether X is a


member of L or not.

8.1 Code

list([1,2,3,4,5,6,7,8,9,10]). check(X):-list(L),member(X,L).

ques8:- write("Enter Number to check in Given List (1,2,3,4,5,6,7,8,9,10) :-

"),read(A),check(A),nl.

8.2 Output

10 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

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

9.1 Code
concat([], L,

L).

concat([H|T], L, [H|L1]) :-

concat(T, L, L1).

ques9:- write("Enter the first

list : "), read(X),

write("Enter Second List : "),

read(Y), write("concat

list:"), concat(X,Y,L),

write(L).

9.2 Output

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

10.1 Code

11 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

reverse_list([], []).

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

reverse_list(T, R1),

append(R1, [H], R).

ques10:-

write("Enter List : "),

read(X),

write("reverse list"),

reverse_list(X,R),

write(R).

10.2 Output

11. Write a program in PROLOG to implement palindrome (L) which checks


whether a list L is a palindrome or not.

11.1 Code

check([H|T],[X|Y]):-H=:=X,check(T,Y). check([_H|_T],[_X|_Y]):-write("Not

a Plaindrome"). check([],[]):-write("Palindrome").

ques11:- write("Enter the list :"),

read([H|T]), reverse([H|T],[X|

Y]),check([H|T],[X|Y]).

12 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

11.2 Output

12. Write a Prolog program to implement sumlist(L, S) so that S is the sum of a


given list L.

12.1 Code

sumlist([H|T],S):- C is H+S,sumlist(T,C).

sumlist([],S):- write("Sum of List is :-"),write(S),nl. sumlist([H|T]):-

S is H,nl,sumlist(T,S).

ques12:- write("Enter

the list :"), read([H|T]),

write("sum of entered list

:"), sumlist([H|T]).

12.2 Ouput

13 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

13. Write a Prolog program to implement two predicates evenlength(List) and

oddlength(List) so that they are true if their argument is a list of even or odd

length respectively.

13.1 Code
len([],0). len([_H|T],A):-len(T,A1),A

is A1+1.

evenlength([H|T]):-len([H|T],L),S is L mod 2,checkeven(S).

checkeven(X):- X=:=0,write("List is of Even Length").

oddlength([H|T]):-len([H|T],L),S is L mod 2,checkodd(S).

checkodd(X):- X=:=1,write("List is of Odd Length :- True").


checkodd(_X):- write("List is of Odd Length :- False").

ques13:-

write("Enter List :"),

14 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

read([H|T]),

evenlength([H|T]),

write(' \n '), oddlength([H|

T]).

13.2 Output

14. Write a Prolog program to implement nth_element (N, L, X) where N is the


desired position, L is a list and X represents the Nth element of L.

14.1 Code

show(N,H):-nl,write("Element at Position "),write(N),write(" in List :-"),write(H).

nth_element(N,[H|T]):- nth_element(N,[H|T],1). nth_element(N,[H|_T],C):-

N=:=C,show(N,H). nth_element(N,[_H|T],C):- C<N,S is

C+1,nth_element(N,T,S).

ques14:- write("Enter the

list : "), read([H|T]),

15 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

write("Enter the positon :"),

read(N), nth_element(N,[H|

T]).

14.2 Output

15. Write a Prolog program to implement maxlist(L, M) so that M is the


maximum number in the list.

15.1 Code

max([H|T],M):- H>M,max(T,H). max([H|T],M):-

H<M,max(T,M).

max([],M):- M>=0,write("Greatest number is :- "),write(M),nl. maxList([]):-write("List

can't be Empty").

maxList([H|T]):- M is H,max(T,M),nl.

ques15:- write("Enter the list :"),

read([H|T]), write("max number in the

given list is :"), maxList([H|T]).

16 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

15.2 Output

16. Write a prolog program to implement insert_nth (I, N, L, R) that inserts an


item I into Nth position of list L to generate a list R.

16.1 Code

show([H|T]):-nl,write("List is :- "),write([H|T]). insert_nth(I,_N,[]):-

write("List is Empty, Inserting in Begining."),show([I]). insert_nth(I,N,[H|

T]):- N=:=1,append([I],[H|T],R),show(R). insert_nth(I,N,[H|T]):-

insert_nth(I,N,T,2,[H]).

insert_nth(I,N,[H|T],C,[H1|T1]):- C=:=N,append([H1|T1],[I],R),append(R,[H|T],V),write("Successfully
inserted"),show(V).

insert_nth(I,N,[H|T],C,[H1|T1]):- C<N,S is C+1,append([H1|T1],[H],V),insert_nth(I,N,T,S,V).

insert_nth(I,_N,[],_C,[H1|T1]):- append([H1|T1],[I],R),write("Successfully inserted"),show(R). ques16:-

write("Enter Position :- "),read(P),nl,write("Enter Number :- "),read(N),nl,write("Enter List :-

"),read(L),nl,insert_nth(N,P,L),nl.

16.2 Output

17 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

17. Write a Prolog program to implement delete_nth (N, L, R) that removes the
element on Nth position from a list L to generate a list R.

17.1 Code

% showing list show([_H|T]):-[Link]("List

is :- "),write(T).

% calculating length of list len([],0).

len([_H|T],A):-len(T,A1),A is A1+1.

removelast([H|T]):- reverse([H|T],[_H1|T1]),reverse(T1,[_X|Y]),write("List is :- "),write(Y).

delete_nth(_N,[]):-write(" Nothing to Delete : List is Empty (UnderFlow).").

delete_nth(N,[H|T]):- len([H|T],L),delete_nth(N,[H|T],1,[_],L). delete_nth(N,[_H|T],C,[H1|T1],_L):-

C=:=N,append([H1|T1],T,V),write("Successfully Deleted"),show(V). delete_nth(N,[H|T],C,[H1|T1],L):-

C<N,S is C+1,append([H1|T1],[H],V),delete_nth(N,T,S,V,L). delete_nth(N,[H|T],_C,[_H1|_T1],L):-

N=:=L,removelast([H|T]). ques17:- write("Enter Index to be Deleted :- "),read(P),nl,write("Enter List :-

"),read(L),nl,delete_nth(P,L).

17.2 Output

18 | 2 0 0 5 9 5 7 0 0 2 2
PRACTICAL FILE - Core Paper XIII: Artificial Intelligence

18. Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is
first ordered list and L2 is second ordered list and L3 represents the merged list.

18.1 Code
show([_H|T]):- write("List is :- "),write(T),nl. merge([H|T],[X|Y],[H1|T1]):-

H>X,append([H1|T1],[X],R),merge([H|T],Y,R). merge([H|T],[X|Y],[H1|T1]):-

X>H,append([H1|T1],[H],R),merge(T,[X|Y],R). merge([H|T],[X|Y],[]):-H>X,R is

[X],merge([H|T],Y,R). merge([H|T],[X|Y],[]):-X>H,R is [H],merge(T,[X|Y],R).

merge([],[X|Y],[H1|T1]):- append([H1|T1],[X|Y],R),show(R). merge([X|Y],[],[H1|T1]):-

append([H1|T1],[X|Y],R),show(R).

ques18:- write("Enter List 1 :- "),read(L1),nl,write("Enter List 2 :- "),read(L2),nl,merge(L1,L2,Temp),nl.

18.2 Output

THANK YOU

19 | 2 0 0 5 9 5 7 0 0 2 2

You might also like