Вы находитесь на странице: 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

Q.5 Disease Problem CODE:


symptom(kumar,nausea). symptom(kumar,headache). symptom(kumar,vomitting). symptom(kumar,fever). problem(Patient,migrane):-symptom(Patient,nausea),symptom(Patient,headache). problem(Patient,viral):-symptom(Patient,headache),symptom(Patient,fever). problem(Patient,typhoid):-symptom(Patient,headache),symptom(Patient,vomitting).

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

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:


checkpalindrome(Line):Line1 = Line, findrev(Line1,[],Rev), write(Rev), Line == Rev. findrev(List, Reversed) :findrev(List, [], Reversed). findrev([], Reversed, Reversed). findrev([Head|Tail], SoFar, Reversed) :findrev(Tail, [Head|SoFar], Reversed).

OUTPUT:

13 | P a g e