0 votes
58 views
by (98.9k points)
Write a Prolog program to calculate the factorial of a number

1 Answer

0 votes
by (98.9k points)
 
Best answer
factorial(0, 1).

factorial(N, Result) :-

    N > 0,

    N1 is N - 1,

    factorial(N1, Result1),

    Result is N * Result1.

 

 

 

You can use this Prolog program to calculate the factorial of a number by calling factorial(N, Result), where N is the number whose factorial you want to calculate, and Result will be unified with the factorial result. For example:

 

?- factorial(5, Result).

Result = 120.

 

Related questions

0 votes
1 answer 307 views
0 votes
1 answer 1.4k views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

497 users

...