You are on page 1of 2

1.Write a program in prolog to show the sum of N natural numbers.

Code:-
summation:-
write("enter the limit:"),read(N),
R is (N+1)*N/2,
write("sum of N natural number is:"),write(R).

2. Write a program in prolog to findout the sum of digits of a


number.
Code:-
sumofdigits(X,X):-
X=<10.
sumofdigits(X,Y):-
X>=10, X1 is X//10, X2 is X mod 10, sumofdigits(X1,Y1), Y is
Y1+X2.

3. Write a program in prolog to findout factorial of a number.


Code:-
fact(0,X):-
X is 1.
fact(N,X):-
N1 is N-1,
fact(N1,X1),
X is X1*N.
Output :

You might also like