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

1.write A Program in Prolog To Show The Sum of N Natural Numbers. Code

The document contains code snippets in Prolog to solve three problems: 1) Finding the sum of the first N natural numbers using a formula. 2) Finding the sum of the digits of a number by recursively breaking it into digits and summing them. 3) Calculating the factorial of a number by recursively multiplying it by all positive integers smaller than it.

Uploaded by

Sumit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views

1.write A Program in Prolog To Show The Sum of N Natural Numbers. Code

The document contains code snippets in Prolog to solve three problems: 1) Finding the sum of the first N natural numbers using a formula. 2) Finding the sum of the digits of a number by recursively breaking it into digits and summing them. 3) Calculating the factorial of a number by recursively multiplying it by all positive integers smaller than it.

Uploaded by

Sumit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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