1 Show Arithmetic Operations Add, Subtract, Multiply, Divide CODE:
add(M,N,Sum):- Sum is M+N. sub(M,N,Diff):- Diff is M-N. mul(M,N,Pro):- Pro is M*N. div(M,N,Quo):- Quo is M/N. exp(M,N,Expo):- Expo is M^N.
OUTPUT:
1|Page
Q.2 Find GCD & LCM CODE:
gcd(n,n). gcd(n,n,res). gcd(N,M,GCD):- N=M,GCD=N. gcd(N,M,GCD):- N<M, AB is M-N, gcd(N,AB,GCD). gcd(N,M,GCD):- N>M, BA is N-M, gcd(BA,M,GCD). lcm(X,Y,LCM):- gcd(X,Y,GCD), LCM is (X*Y)/GCD.
OUTPUT:
2|Page
Q.3 To find Factorial of a no. CODE:
fact(0,1). fact(N,Fact) :-N>0,N1 is N-1, fact(N1,D),Fact is N*D.
fib(m,i,n). fib(M,I,N):L is M+I, write(L), N1 is N-1, N1>0, write('\n'), fib(I,L,N1).
OUTPUT:
8|Page
Q.8 Print table of a number. CODE:
table(N,M):- table(N,0,M). table(N,I,M):write(N), write('X'), write(I), write('='), X is N*I, write(X), write('\n'), I1 is I+1, M1 is M+1, I1<M1, table(N,I1,M).
OUTPUT:
9|Page
Q.9 Find the area. CODE:
area(circle,Radius, Area):- Area is 3.14 * Radius * Radius. area(square,Side, Area):- Area is Side * Side. area(rectangle,Length, Breadth, Area):- Area is Length * Breadth.
OUTPUT:
10 | P a g e
Q.10 Find the percentage. CODE:
percentage:write('Enter Marks :-'),nl, write('Maths='),read(M),nl, write('Physics='),read(S),nl, write('chemistry='),read(C),nl, P is (M+S+C)/3, write('your percentage is='), write(P), write('%').
OUTPUT:
11 | P a g e
Q.11 Find the length of the list. CODE:
readlist([],Len):- write('The length is '),write(Len). readlist([First|Rest],Len):- Len1 is Len + 1, readlist(Rest,Len1). findsize(List):- readlist(List,0).
OUTPUT:
12 | P a g e
Q.12 To reverse the list and checking if palindrome. CODE: