Pages

Monday, 31 October 2016

Write a Prolog program to calculate the factorial of a given number.


% Write a Prolog program to calculate the factorial of a given number.
fact(0,1).
fact(N,F):-
(

 % The below is for +ve factorial.
 N>0 ->
 (
  N1 is N-1,
  fact(N1,F1),
  F is N*F1
 )
 ;
 
 % The below is for -ve factorial.
 N<0 ->
 (
  N1 is N+1,
  fact(N1,F1),
  F is N*F1
 )
).

% Output

Prolog Factorial Program
Prolog Factorial Program


Video on prolog program to find the factorial of a number.


3 comments:

  1. Nice Post! I couldn't even figure how to compile prolog code. You helped a lot!

    ReplyDelete
    Replies
    1. Please look at the Output Image, where the command for compiling this Prolog Program is clearly written. Also, this and all the related posts assume that you have a minimum experience with Prolog. If not, you can always go and search for resources for the same on any Search Engine.
      Hope this helps.

      Delete
  2. Great information help us to understand how to write Prolog program to calculate the factorial of any number. Thanks for sharing.
    Best Artificial Intelligence course in Mumbai

    ReplyDelete