Вы находитесь на странице: 1из 13

Q.

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.

OUTPUT:

3|Page

Q.4 Find the Relationship CODE:

parent(pat,mike). parent(pat,julie). parent(pat,amanda). parent(marie,mike). parent(marie,julie). parent(marie,amanda). female(marie). female(julie). female(amanda). male(mike). male(pat). sibling(mike,julie). sibling(mike,amanda). sibling(amanda,mike). sibling(julie,mike). sibling(julie,amanda). sibling(amanda,julie). father(X,Y):-male(X),parent(X,Y). mother(X,Y):-female(X),parent(X,Y). brother(X,Y):-male(X),sibling(X,Y),X\=Y. sister(X,Y):-female(X),sibling(X,Y),X\=Y.

4|Page

OUTPUT:

5|Page

OUTPUT:

6|Page

Q.6 Print a string N times. CODE:

string(a,b). string(S,N):-write(S),nl,M=N-1,M>0 ,string(S,M).

OUTPUT:

7|Page

Q.7 Print next number of Fibonacci series. CODE:

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

OUTPUT:

10 | P a g e

OUTPUT:

11 | P a g e