0% found this document useful (0 votes)
25 views

AI Assignment

The document contains three Prolog programs. The first program calculates the sum of N natural numbers. The second program finds the sum of digits of a number. The third program calculates the factorial of a number.

Uploaded by

Sumit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

AI Assignment

The document contains three Prolog programs. The first program calculates the sum of N natural numbers. The second program finds the sum of digits of a number. The third program calculates the factorial of a number.

Uploaded by

Sumit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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